Show records using mysqli_fetch_object( ) function
mysqli_fetch_object() return the rows from the number of records available in the database as an object. at a time it return only the first row as an object. if we want to retrieve all the records of the table then we must put this function inside the while loop.
mysqli_fetch_object(query)
<?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);
$results=mysqli_fetch_object($result);
echo $results->name;
mysqli_close();
?>
Rakesh
<?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->id."</td>";
echo "<td>".$row->name."</td>";
echo "<td>".$row->salary."</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "0 results";
}
mysqli_close($conn);
?>
id | name | salary |
---|---|---|
1 | Name1 | 55000 |
2 | Name2 | 70000 |
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning