Home >>MySQL Tutorial >Mysqli Fetch Array

Mysqli Fetch Array

Display data using mysqli_fetch_array( ) function

mysqli_fetch_array() return the rows from the number of records available in the database as an associative array or numeric array. At a time it return only the first row as an associative array or numeric array. if we want to retrieve all the records of the table then we must put this function inside the while loop. Syntax

  mysql_fetch_array(data)

Display the records using mysqli_fetch_array()

 <?php
//database connectivity
$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_array($result));
mysqli_close($con);
?>
Output :
  Array(
			[0] =>1 
			[emp_id] => 
			[1] => devesh
			[Name] => devesh
			[2] => devesh@gmail.com
			[email] =>devesh@gmail.com
			[3] => 9910099100
			[mobile] => 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_array() and the data of the associative array or numeric array is displayed .

Display all records from empInfo table using mysqli_fetch_array( ) 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_array($val))
{
echo $r['emp_id']." ".$r['name']." ".$r['email']." ".$r['mobile']."<br/>";
}				
?>
Output :
Emp_id Name Email 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_array() and array 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.


No Sidebar ads