When using PHP for database operations, the mysqli extension provides very convenient functions to connect, query and operate databases. mysqli_result is the result set object returned when the query is executed. This article will introduce how to correctly traverse all the results returned by the mysqli_result function so that query results can be efficiently obtained and processed.
First, we need to connect to the database through the mysqli_connect() function. Assume here that you already have basic MySQL database operation experience, the following is a simple database connection example:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
After connecting to the database, we can execute SQL queries through mysqli_query() . Suppose we query a table named users and get information about all users:
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
At this point, $result will be a mysqli_result object containing all the data of the query.
The most common way to traverse the result set returned by mysqli_result is to use the mysqli_fetch_assoc() function. The function returns an associative array, and each time it is called it returns a row of data and moves the pointer to the next row until there is no more data.
if ($result->num_rows > 0) {
// Output data for each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 result";
}
In this example, mysqli_fetch_assoc() returns an associative array where the key of the array is the field name and the value is the value of the corresponding field.
In addition to mysqli_fetch_assoc() , you can also use mysqli_fetch_row() to return an index array, and the field values of each row of data will be stored in the array in order. When using this method, you need to access the values of each field through the index.
if ($result->num_rows > 0) {
// Output data for each row
while($row = $result->fetch_row()) {
echo "id: " . $row[0] . " - Name: " . $row[1] . " - Email: " . $row[2] . "<br>";
}
} else {
echo "0 result";
}
Here mysqli_fetch_row() returns a numeric index array, $row[0] corresponds to the id field, $row[1] is name , and $row[2] is email .
If you are more accustomed to the way objects are used to process query results, you can use mysqli_fetch_object() . It returns an object, each field can be accessed through the object properties.
if ($result->num_rows > 0) {
// Output data for each row
while($row = $result->fetch_object()) {
echo "id: " . $row->id . " - Name: " . $row->name . " - Email: " . $row->email . "<br>";
}
} else {
echo "0 result";
}
If you need to manipulate data on all rows more flexibly during processing, you can use the mysqli_fetch_all() function, which returns a two-dimensional array containing all results.
$allResults = $result->fetch_all(MYSQLI_ASSOC);
foreach ($allResults as $row) {
echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
Here fetch_all(MYSQLI_ASSOC) will return all results as an associative array.
After the operation is completed, remember to close the database connection:
$conn->close();
Through the above methods, we can iterate through all the results returned by the mysqli_result function. Which method to use depends on your individual needs and how the data is processed. If you want to get fields by associating arrays, you can use mysqli_fetch_assoc() ; if you prefer object-oriented, you can choose mysqli_fetch_object() ; if you need to process all rows of data at the same time, you can use mysqli_fetch_all() .
Remember, after the database connection operation, close the connection in time to ensure the release of resources.