W3docs

fetch_row

In this article, we will focus on the mysqli_fetch_row() function in PHP, which is used to fetch the next row of a MySQLi result set as an enumerated array. We

In this article, we will focus on the mysqli_fetch_row() function in PHP, which is used to fetch the next row of a MySQLi result set as an enumerated array. We will provide you with an overview of the function, how it works, and examples of its use.

Introduction to the mysqli_fetch_row() function

The mysqli_fetch_row() function is a built-in function in PHP that is used to fetch the next row of a MySQLi result set as an enumerated array. This function is useful when you need to access the columns of a row in a result set by their numerical index.

How to use the mysqli_fetch_row() function

Using the mysqli_fetch_row() function is very simple. You just need to call the function on a valid MySQLi result set. Here is an example:

How to use the mysqli_fetch_row() function?

<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

if (!$mysqli) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);

if ($result) {
    while ($row = mysqli_fetch_row($result)) {
        printf("%s (%s)\n", $row[0], $row[1]);
    }
} else {
    echo "Query failed: " . mysqli_error($mysqli);
}

mysqli_close($mysqli);
?>

In this example, we call the mysqli_connect() function to connect to a MySQL database. We check if the connection succeeded before proceeding. Next, we execute a query using the mysqli_query() function to select all columns from a table. We store the result in a variable and verify it. If the query succeeds, we use a while loop to iterate over each row. The mysqli_fetch_row() function returns an enumerated array for each row, and returns null when no more rows remain, which naturally terminates the loop. For better readability in modern PHP, consider using mysqli_fetch_assoc() instead, as it returns an associative array keyed by column names rather than numerical indices.

Conclusion

In conclusion, the mysqli_fetch_row() function is a useful tool for fetching the next row of a MySQLi result set as an enumerated array. By understanding how to use the function, you can take advantage of this feature to create powerful and flexible MySQLi queries.

Practice

Practice

Which of the following are true about the PHP function mysqli_fetch_row()?