Current Location: Home> Function Categories> mysqli_result::fetch_assoc

mysqli_result::fetch_assoc

(mysqli_fetch_assoc) Get the result row as an associative array
Name:mysqli_result::fetch_assoc
Category:MySQLi
Programming Language:php
One-line Description:Get the result row as an associative array.

Definition and usage

fetch_assoc() / mysqli_fetch_assoc() function gets the result row in the form of an associative array.

Note: The field names returned from this function are case sensitive.

Example

Example 1 - Object-Oriented Style

Get the result row in the form of an associative array:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname" ;
$result = $mysqli -> query ( $sql ) ;

// Associate array
$row = $result -> fetch_assoc ( ) ;
printf ( "%s (%s)\n" , $row [ "Lastname" ] , $row [ "Age" ] ) ;

// Release the result set
$result -> free_result ( ) ;

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Get the result row in the form of an associative array:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;
if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname" ;
$result = mysqli_query ( $con , $sql ) ;

// Associate array
$row = mysqli_fetch_assoc ( $result ) ;
printf ( "%s (%s)\n" , $row [ "Lastname" ] , $row [ "Age" ] ) ;

// Release the result set
mysqli_free_result ( $result ) ;

mysqli_close ( $con ) ;
?>

grammar

Object-oriented style:

 $mysqli_result -> fetch_assoc ( )

Procedural Style:

 mysqli_fetch_assoc ( result )
parameter describe
result Required. Specifies the result set identifier returned by mysqli_query(), mysqli_store_result(), or mysqli_use_result().
Similar Functions
Popular Articles