Home >>MySQL Tutorial >Mysqli fetch object
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.
Syntaxmysqli_fetch_object(data)
<?php //connect database $con=mysqli_connect("localhost","root","","Employee") or die(mysqli_error()); //select table $sql = "SELECT * from empInfo WHERE email='devesh@gmail.com'"; $result = mysqli_query($con,$sql); $row=mysqli_fetch_object($result); echo $row->name; mysqli_close($con); ?>
Output : Devesh
<?php //connect database $con=mysqli_connect("localhost","root","","Employee") or die(mysqli_error()); //select all values from empInfo table $data="SELECT * FROM empInfo"; $val=mysqli_query($con,$data); while($row=mysqli_fetch_object($val)) { echo $row->emp_id." ".$row->name." ".$row->email." ".$row->mobile."<br/>"; } mysqli_close($con); ?>
Emp_id | Name | Mobile | |
---|---|---|---|
1 | devesh | devesh@gmail.com | 9910099100 |
2 | deepak | deepak@gmail.com | 9210053520 |
3 | ravi | ravi@gmail.com | 9810098100 |