The mysqli_fetch_row()
function in PHP is used to fetch a single row from a result set as a numeric array (i.e., the array keys are numbers starting from 0). It is typically used after executing a SELECT
query with the mysqli_query()
function.
🔹 Syntax:
mysqli_fetch_row(mysqli_result $result): array|false
- $result: The result set returned by
mysqli_query()
. - Returns: An enumerated array (indexed numerically) of strings that corresponds to the fetched row, or
false
if there are no more rows.
✅ Example 1: Basic Usage
<?php
$mysqli = new mysqli("localhost", "root", "", "testdb");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Run a SELECT query
$result = $mysqli->query("SELECT id, name, email FROM users");
if ($result) {
while ($row = mysqli_fetch_row($result)) {
echo "ID: $row[0] | Name: $row[1] | Email: $row[2]<br>";
}
} else {
echo "Query failed: " . $mysqli->error;
}
$mysqli->close();
?>
🔸 Output:
ID: 1 | Name: John | Email: john@example.com
ID: 2 | Name: Alice | Email: alice@example.com
...
✅ Example 2: Fetching One Row Only
<?php
$result = $mysqli->query("SELECT id, name FROM users LIMIT 1");
$row = mysqli_fetch_row($result);
echo "ID: " . $row[0] . " | Name: " . $row[1];
?>
⚠️ Notes:
- Use
mysqli_fetch_row()
when you want numeric indices only. - If you prefer associative keys, use
mysqli_fetch_assoc()
. - If you want both numeric and associative, use
mysqli_fetch_array()
.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.