Jump to content

[SOLVED] Print/echo all returned MySQLi rows


cgm225

Recommended Posts

Currently, I have some MySQLi code (shown below) that returns rows based on a prepared statement, of which I print out the first entry.  My question is, how do I print/echo ALL the returned rows... currently, it only does the first one.

 

Thanks in advance!

 

$stmt = $mysqli->prepare("SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC");
$stmt->execute();
$stmt->bind_result($id, $title, $note, $date, $timestamp);
$stmt->fetch();
printf("%s %s %s %s %s\n", $id, $title, $note, $date, $timestamp);
$stmt->close();

 

I haven't done a whole lot with MySQLi, but it should just be:

 

<?php
$stmt = $mysqli->prepare("SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC");
$stmt->execute();
$stmt->bind_result($id, $title, $note, $date, $timestamp);
while($stmt->fetch())
  printf("%s %s %s %s %s\n", $id, $title, $note, $date, $timestamp);
$stmt->close();
?>

i don't know if this will help, but i'm not a big fan of the short whiles, ifs or anything like that. to maintain consistency, i prefer to block them even if they're only one line. so i'd go with this whether it helps or not.

 

while($stmt->fetch()) {
  printf("%s %s %s %s %s\n", $id, $title, $note, $date, $timestamp);
}

This piece of code works, printing the first retrieved row/entry...

$stmt = $mysqli->prepare("SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC");
$stmt->execute();
$stmt->bind_result($id, $title, $note, $date, $timestamp);
$stmt->fetch();
printf("%s %s %s %s %s\n", $id, $title, $note, $date, $timestamp);    

 

This piece of code does not work, and firefox displays a "Problem loading page" screen...

$stmt = $mysqli->prepare("SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC");
$stmt->execute();
$stmt->bind_result($id, $title, $note, $date, $timestamp);
while($stmt->fetch()) {
     printf("%s %s %s %s %s\n", $id, $title, $note, $date, $timestamp);
}
$stmt->close();

 

 

Is there a problem with my code, or maybe a bigger issue with my php/mysql server settings?

 

Thanks again in advance!

well, if nothing else, i'm learning mysqli while trying to help. this works for me:

 

if ($stmt = $mysqli->prepare("SELECT id, date, bill_street1 FROM orders ORDER BY date ASC LIMIT 5")) { 
    $stmt->execute(); 

    /* bind variables to prepared statement */ 
    $stmt->bind_result($col1, $col2, $col3); 

    /* fetch values */ 
    while ($stmt->fetch()) { 
        printf("%s %s %s<BR>", $col1, $col2, $col3); 
    } 

    /* close statement */ 
    $stmt->close(); 
}

 

got it from here: http://devzone.zend.com/node/view/id/686

 

i don't see anything odd in your code. it looks like it should work properly. perhaps there is an issue in your php/mysql installation.

Try this:

 

<?php
  if($result = $mysqli->query("SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC")){
    while($row = $result->fetch_assoc()) {
      printf("%s %s %s %s %s\n", $row['id'], $row['title'], $row['note'], $row['date'], $row['timestamp']);
    }
    $result->close();
  }else
    printf("MySQLi Error: %s\n", $mysqli->error);
  $mysqli->close();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.