Home >>MySQL Tutorial >Mysqli fetch row
The 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(data)
<?php //connect database $con=mysqli_connect("localhost","root","","Employee") or die(mysqli_error()); //select values from empInfo table $sql = "SELECT * from empInfo WHERE email='devesh@gmail.com'"; $result = mysqli_query($con,$sql); print_r(mysqli_fetch_row($result)); mysqli_close($con); ?>
Output : Array( [0] =>1 [1] => devesh [2] => devesh@gmail.com [3] => 9910099100 )
In the above example first connection to the database is created after that the result provided by mysqli_query() is passed to mysqli_fetch_row() and the data of the row is displayed using print_r() function.
<?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($r=mysqli_fetch_row($val)) { echo $r[0]." ".$r[1]." ".$r[2]." ".$r[3]."<br/>"; } ?>
Emp_id | Name | Mobile | |
---|---|---|---|
1 | devesh | devesh@gmail.com | 9910099100 |
2 | deepak | deepak@gmail.com | 9210053520 |
3 | ravi | ravi@gmail.com | 9810098100 |
In the above example first connection to the database is created after that the result provided by mysqli_query() is passed to mysqli_fetch_row() and row returned is stored in 'r' variable and it put in while loop to fetch all records . after that the data is displayed in tabular form. Related: mysqli fetch array