PHP mysqli_fetch_assoc() Function

PHP mysqli_fetch_assoc() Function is used to fetch rows as an associative array from the database.

Associative arrays are the arrays where the indexes are the names of the individual columns of the table.

PHP Version : PHP 5, PHP 7

Syntax for mysqli_fetch_assoc():

According to Procedural,

mysqli_fetch_assoc($resultset);

According to Object oriented,

$mysqli_result->fetch_assoc();

Parameter,

$resultset : Required. It is a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result().

Return values for mysqli_fetch_assoc():

It returns an associative array of strings representing the fetched row. NULL if there are no more rows in result-set

Example for mysqli_fetch_assoc():

Let's see below example to understand php mysqli_fetch_assoc() Function in details.

<?php
$connection = mysqli_connect("localhost","root","","student");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL server : " . mysqli_connect_error();
  exit();
}

$resultset = mysqli_query($connection , "SELECT roll_no, name, phone FROM student");

 echo " <br> <b>Roll No   Student Name    Phone </b>";

while($row = mysqli_fetch_assoc($resultset , MYSQLI_ASSOC))
{

       echo " <br> ".$row['roll_no']. "  ".$row['name']."  ".$row['phone'];

}

mysqli_free_result($result);

mysqli_close($connection );
?>

Output :

Roll No  Student Name   Phone         
1            Arya                   79453*****   
2            Dirgh                  98785*****   
3            Arav                   93732*****  
 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

60175