Tom Tom - 1 month ago 6
PHP Question

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 />";
}

Answer

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..

Comments