PHP MySQL: Display records using fetch_row()

This entry is part 12 of 17 in the series PHP & MySQL

We have used associative array method to display the table records.  If you preferred, we can also use non-associative array method using fetch_row().

Syntax: mixed mysqli_result::fetch_row (void)

I would recommend that you use the earlier fetch_assoc() as it offers a cleaner coding style.

$mysqli = new mysqli("localhost", "root", "", "allartist");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());

$query = "select * from artist";

if ($result = $mysqli->query($query)) {

    /* fetch object array */
    while ($row = $result->fetch_row()) {
        printf ("%s %s %s %s", $row[0], $row[1], $row[2], $row[3]);
        echo "<br>";

    /* free result set */

/* close connection */

We would usually combine fetch_row with a while loop to retrieve the records in DB.

If we ran the above code, we get the same result as shown in the screenshot below.


Series Navigation<< PHP MySQL: Select to display table using fetch_assoc
PHP MySQL: Display records using fetch_array >>