Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli query results to show all rows

I have the following code:

include $_SERVER['DOCUMENT_ROOT'].'/include/conn.php'; 

$query = "SELECT title FROM news_event";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
$row_cnt = $result->num_rows;
$result->free();
$mysqli->close();

This is fine if there is only one result as I can just echo $row['title'] but if there are lots of results, how do I get this to loop through and print every row?

I'm sure this is really simple but I'm just not sure what I need to search for in Google.

I'm looking for a mysqli equivalent of this:

while( $row = mysql_fetch_array($result) )
{
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
}
like image 549
Tom Avatar asked Sep 28 '12 20:09

Tom


1 Answers

Just replace it with mysqli_fetch_array or mysqli_result::fetch_array :)

while( $row = $result->fetch_array() )
{
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
}

Almost all mysql_* functions have a corresponding mysqli_* function.

like image 174
raidenace Avatar answered Oct 07 '22 22:10

raidenace