Current Location: Home> Latest Articles> Use mysqli_result::fetch_row() to get a numeric index array

Use mysqli_result::fetch_row() to get a numeric index array

M66 2025-05-28

In PHP, when using a MySQL database, the mysqli extension is usually used to perform database operations. The mysqli_result::fetch_row() method is one of the common ways to get a row of data from a database query result set. It returns an array of numeric indexes, where each value corresponds to a column of data returned in the SQL query.

Next, we will explain in detail how to use mysqli_result::fetch_row() to extract numerical index arrays from query results and demonstrate how to implement this in your code.

What is mysqli_result::fetch_row() ?

mysqli_result::fetch_row() is a very useful method in the mysqli class. It takes a row of data from the result set and returns all columns in that row as a numerical index array. Unlike fetch_assoc() to return an associative array, fetch_row() only returns an array composed of numeric indexes.

Use mysqli_result::fetch_row() to get a numeric index array

Here is a simple example showing how to use mysqli_result::fetch_row() to get a numeric index array from query results:

 <?php
// Create a connection
$mysqli = new mysqli("localhost", "username", "password", "Database name");

// Check if the connection is successful
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Execute a query
$query = "SELECT id, name, email FROM users";
$result = $mysqli->query($query);

// Check whether the query is successful
if ($result) {
    // Get each row of data in the query result
    while ($row = $result->fetch_row()) {
        // $row Return an array of numeric indexes
        echo "ID: " . $row[0] . " - Name: " . $row[1] . " - Email: " . $row[2] . "<br>";
    }
} else {
    echo "Query failed: " . $mysqli->error;
}

// Close the connection
$mysqli->close();
?>

Explain the code

  1. Database connection : First, we create a mysqli object to connect to the database, providing the host name, user name, password and database name of the database server.

  2. Execute query : Use the query() method to execute SQL query and return a query result object.

  3. Get data : Use the fetch_row() method to get the query results row by row, and return each row of data as a numerical index array. In this example, the indexes in the array are 0 , 1 and 2 , respectively, corresponding to the id , name and email fields.

  4. Output data : outputs the field values ​​of each row of data through echo .

Things to note

  • The array returned by fetch_row() is a numeric index array, so if you need to access the data of a certain column, you must use the numeric index of that column (starting from 0).

  • After each call to fetch_row() , the next row in the result set will be returned. If there are no more rows, it returns null and can use a while loop to iterate through all results.

  • If the amount of data returned by the query is large, it is recommended to use paging queries to reduce memory usage.

Example of URL replacement in real applications

If your application needs to get data from an external data source and the URL needs to be replaced with the domain name m66.net , it can be achieved by simple string replacement. Here is an example: