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

mysqli_result::fetch_array

(mysqli_fetch_array) Extract the result row into an associated row, an array of numbers, or both
Name:mysqli_result::fetch_array
Category:MySQLi
Programming Language:php
One-line Description:Get the result row as an associative array, an array of numbers, or both.

Definition and usage

fetch_array() / mysqli_fetch_array() functions get the result row as an associative array, an array of numbers, or both.

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

Example

Example 1 - Object-Oriented Style

Get the result row as an array of numbers and 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 ) ;

// Array of numbers
$row = $result -> fetch_array ( MYSQLI_NUM ) ;
printf ( "%s (%s)\n" , $row [ 0 ] , $row [ 1 ] ) ;

// Associate array
$row = $result -> fetch_array ( MYSQLI_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 as an array of numbers and 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 ) ;

// Array of numbers
$row = mysqli_fetch_array ( $result , MYSQLI_NUM ) ;
printf ( "%s (%s)\n" , $row [ 0 ] , $row [ 1 ] ) ;

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

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

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles