mysqli_result::fetch_row is a method in the MySQLi extension, used to retrieve a row of data from a result set. The return value is an indexed array containing the data from that row, with each element corresponding to a field value in the query result. In cases where single-column data is retrieved, it usually returns an array containing a single data element.
Example code:
<?php
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");
<p>// Check the connection<br>
if ($mysqli->connect_error) {<br>
die("Connection failed: " . $mysqli->connect_error);<br>
}</p>
<p>// Perform a query<br>
$result = $mysqli->query("SELECT username FROM users");</p>
<p>// Retrieve single-column data<br>
$row = $result->fetch_row();<br>
echo $row[0]; // Output the first column data, i.e., username<br>
?><br>
In this example, $row[0] is the data from the username column. The fetch_row function is very useful for retrieving single-column or single-row data.
Suppose we query a column with multiple usernames; how do we process this data? We can iterate over each row or store the results in an array for further operations.
If the query result is a single column with multiple rows, such as retrieving all usernames, you can use fetch_row in conjunction with a while loop to iterate through each row:
<?php
// Query all usernames
$query = "SELECT username FROM users";
$result = $mysqli->query($query);
<p>// Store all usernames<br>
$usernames = [];<br>
while ($row = $result->fetch_row()) {<br>
$usernames[] = $row[0];<br>
}</p>
<p>// Output all usernames<br>
foreach ($usernames as $username) {<br>
echo $username . "<br>";<br>
}<br>
?><br>
Once the data is extracted into an array, you can perform various operations such as filtering, sorting, or removing duplicates. For example, if you want to filter out usernames containing a specific keyword, you can do it like this:
<?php
// Filter usernames containing "admin"
$filtered_usernames = array_filter($usernames, function($username) {
return strpos($username, 'admin') !== false;
});
<p>// Output filtered usernames<br>
foreach ($filtered_usernames as $username) {<br>
echo $username . "<br>";<br>
}<br>
?><br>
Using the array_filter function, you can easily process single-column data retrieved from the database.
Sometimes you need to display the extracted data on a web page or pass it to the front-end via URL. If you need to pass the data to an HTML tag, here's how you can embed it into HTML:
<?php
// Output user list to HTML
echo "<ul>";
foreach ($usernames as $username) {
echo "<li><code>m66.net/{$username}
Here, we embed a tag inside each
Tag
If the data needs to be combined with a URL, you can directly generate the URLs inside the tag. For example:
<?php
// Output each user's homepage link
foreach ($usernames as $username) {
echo "<p>User homepage: <code>https://m66.net/{$username}
In this example, each user's personal homepage is generated with the domain m66.net, dynamically creating a full URL and displaying it formatted inside the tag.
Error Handling: When using fetch_row, if the query result is empty or an error occurs, the return value will be NULL. You can check the return value of fetch_row to ensure you're processing valid data:
$row = $result->fetch_row();
if ($row) {
// Process the data
echo $row[0];
} else {
// Handle no data found
echo "No data found";
}
Performance Optimization: When dealing with large data sets, it is recommended to use pagination or limit the number of rows returned by the query to avoid excessive memory usage.
Through this article, you should now understand how to use the mysqli_result::fetch_row function to retrieve and process single-column data, as well as how to apply this data to front-end pages. We hope these examples help you with database operations and data presentation.