mysqli query results to show all rows

Solution 1:

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.

Solution 2:

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();

Solution 3:

Use:

while ($row = $result->fetch_array(MYSQLI_BOTH)) {
    // Look inside $row here, do what you want with it.
}

Look at the associative array examples here (should correspond with fetch_array() versions as well):

http://php.net/manual/en/mysqli-result.fetch-assoc.php

Solution 4:

You can simply loop using foreach

$query = "SELECT title FROM news_event";
$result = $mysqli->query($query);
foreach($result as $row) {
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
}

If you would like to keep the mysqli code and HTML separate, then it's good idea to fetch all into an array and loop later.

$query = "SELECT title FROM news_event";
$result = $mysqli->query($query);
$news_events = $result->fetch_all(MYSQLI_ASSOC);

// Later in HTML:    

<div>
<?php foreach($news_events as $row): ?>
    <p><?= $row['FirstName'] ?> <?= $row['LastName'] ?></p>
<?php endforeach ?>
</div>