mysqli query results to show all rows
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.
Simple mysqli solution:
$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT * FROM table WHERE 1');
while ( $rows = $resource->fetch_assoc() ) {
print_r($rows);//echo "{$row['field']}";
}
$resource->free();
$db->close();
With Error Handling: If there is a fatal error the script will terminate with an error message.
// ini_set('display_errors',1); // Uncomment to show errors to the end user.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT field FROM table WHERE 1');
while ( $row = $resource->fetch_assoc() ) {
echo "{$row['field']}";
}
$resource->free();
$db->close();
Using iterators: Support was added with PHP 5.4
$db = new mysqli('localhost','user','password','database');
foreach ( $db->query('SELECT * FROM table') as $row ) {
print_r($row);//echo "{$row['field']}";
}
$db->close();
Fetch a single record: This code does not require a loop.
$db = new mysqli('localhost','user','password','database');
$resource = $db->query('SELECT field FROM table');
$row = $resource->fetch_assoc();
echo "{$row['field']}";
$resource->free();
$db->close();