MySQLi Fetch Row Using PHP
Show records using mysqli_fetch_row( ) function
mysqli_fetch_row() function returns a row from a recordset as a numeric array. mysqli_fetch_row() return a single row from the number of records available in the database. at a time it return only the first row of the result set. if we want to retrieve all the rows of the table then we must put this function inside the while loop.
Syntax:
mysqli_fetch_row(query)
PHP MySQLi Select Query Using mysqli_fetch_row() With Output Example
<?php
$host = "localhost";
$user = "";
$pass = "";
$dbname = "users";
$conn = mysqli_connect($host,$user,$pass,$dbname) or die(mysqli_error($conn));
$sql = "SELECT * FROM users";
$result=mysqli_query($conn,$sql);
print_r(mysqli_fetch_row($result));
mysqli_close($conn);
?>
Output:
Array(
[0] =>1
[1] => rakesh
[2] =>20000
)
Show all records from users table using mysqli_fetch_row( ) function:
<?php
$host = "localhost";
$user = "";
$pass = "";
$dbname = "users";
$conn = mysqli_connect($host,$user,$pass,$dbname) or die(mysqli_error($conn));
$sql = "SELECT * FROM users";
$retval=mysqli_query($conn,$sql);
if(mysqli_num_rows($retval) > 0){
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Salary</th></tr>";
while($row= mysqli_fetch_row($retval))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "0 results";
}
mysqli_close($conn);
?>
Output:
Result In Table: Users
| id | name | salary |
|---|---|---|
| 1 | Name1 | 55000 |
| 2 | Name2 | 70000 |
Series:
MySql Tutorials Hub
