Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

mysqli prepare statements und smarty

 
Post new topic   Reply to topic    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Thu Sep 10, 2015 11:03 pm    Post subject: mysqli prepare statements und smarty Reply with quote

Hi,

Ich bin Anfängen im PHP Bereich und wollte mich nun in mysqli prepare statements einarbeiten das ganze hat bei den ersten Versuchen auch super geklappt um singleRows auszulesen.

Jedoch jetzt wo ich ganze Tabellen (Limit 15) auslesen möchte ich Sense und komm nicht weiter und weiss nicht wo ansetzen:

Code:
// GET COMMENTS
$query = "SELECT * FROM comments WHERE id=? LIMIT 15";

// Setup parameter to be bound into query
$strainID = $_GET['strainID'];

// Get instance of statement
$stmt = $con->stmt_init();

// Prepare Query
if($stmt->prepare($query)){

// Bind Parameters [s for string]
$stmt->bind_param("i", $strainID);

// Execute statement
$stmt->execute();
$stmt->store_result();

// Bind result variables
$stmt->bind_result($bId, $bDate, $bName);

// Fetch Value
$stmt->fetch();

// Close Statement
$stmt->close();
}


Das ganze möchte ich dann so im tpl ausgeben
Code:
{foreach $rowsComments as $rowComments}
{$rowComments.id} - {$rowComments.date} {$rowComments.name}
{/foreach}


und dem tpl so zuweisen:
Code:
$smarty->assign('rowsComments', $rowsComments);


Wäre dankbar wennn mir hier jemand unter die Arme greifen könnte um mir zu zeigen wie ich die ganze tabelle hier auslese und ans tpl weitergebe.

Danke[/code]
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Thu Sep 10, 2015 11:47 pm    Post subject: Reply with quote

Code:
$_rc = $_pdo->prepare("SELECT * FROM comments WHERE id=? LIMIT 15");
$_rc->execute(array($_GET['strainID']));
$smarty->assign('rowsComments', $_rc->fetchAll(PDO::FETCH_ASSOC));


http://php.net/PDO
Back to top
View user's profile Send private message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Fri Sep 11, 2015 12:03 am    Post subject: Reply with quote

Hi,

Ich arbeite hier aber nicht mit PDO sondern mysqli ? Smile

EDIT

Habe es denoch jetzt mit PDO versucht klappt aber nicht

Code:
$connection = new PDO("mysql:host=localhost;dbname=XXX", "new_user", "XXX");
$_rc = $connection->prepare("SELECT * FROM comments WHERE id=? LIMIT 15");
$_rc->execute(array($_GET['strainID']));
$smarty->assign('rowsComments', $_rc->fetchAll(PDO::FETCH_ASSOC));


Code:
{foreach $rowsComments as $rowComment}
 {$rowComment.strainID}
{/foreach}


Wird aber nix ausgegeben?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Fri Sep 11, 2015 1:45 am    Post subject: Reply with quote

There's almost no reason to use MySQLi directly in any form.
Only very, very specific use case could warrant such trouble.

Onward to your issue, "it doesn't work" doesn't tell me anything to help you understand your issue, but your code, indeed, do provide some clues.

First your mistake is that you do not check, if you've successfully connected to a database. Nor you tell PHP to tell you, if anything goes wrong.
It is still probably went ok', but we don't know for sure.
Here's my generic stub for this kind of task:
Code:

  // PDO driver connection
  $_pdo = new PDO($DSN, $__s['SQLUSER'], $__s['SQLPWD'],
    array( // Driver-agnostic options
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      // MySQL exclusive stuff - PHP < 5.3.6
      // For PHP 5.3.6 and above, use the "charset=" DSN attribute
      // to specify connection charset!
      PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES cp1251;',
      // If you want server-side prepares (slow and potentially unstable on loaded server)
//      PDO::ATTR_PERSISTENT => true,
  // Finishing
    ));

This will make PDO throw exceptions if anything hairy happens, so you don't need to waste your time checking results of every execution. Any issues will sink down to any exception handling logic you have in place.

Second problem is that you don't know if the value of "$_GET['strainID']" contain anything that could fetch data from the database. Manually assign some value that you KNOW would yield positive results, make sure your code works, then change it to an indirect variable.

Third problem: Start using debugging tools. Both built into Smarty and external, like xdebug.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP