MySQLi Function in PHP: Part 4

In this article I describe the PHP MySQLi functions mysqli_fetch_all, mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_field_direct and mysqli-fetch_field.

Introduction

In this article I describe the PHP MySQLi  functions mysqli_fetch_all, mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_field_direct and mysqli-fetch_field. To learn some other MySQLi functions, go to:

  1. MySQLi Function in PHP: Part 1
  2. MySQLi Function in PHP: Part 2
  3. MySQLi Function in PHP: Part 3

PHP mysqli_fetch_all() Function

The PHP "MySQLi mysqli_fetch_all" function fetches all result rows an an associative array, a numeric array or both and this function returns an array of associative or numeric arrays holding result rows.

Syntax

mysqli_fetch_all(result, resulttype)

Parameters of the mysqli_fetch_all function

The parameters of the function are: 

Parameter Description
result It specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_user_result().
resulttype It specifies what type of array that should be produce. Can be one of the following values:
  • MySQLI_ASSOC
  • MYSQLI_NUM
  • MYSQLI_BOTH

Example

An example of the function is:

<?php

$con=mysqli_connect("localhost","root","","mysql");

if (mysqli_connect_errno($con))

  {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

  }

$result=mysqli_query($con,"select * from emp");

// Fetch all

mysqli_fetch_all($result,MYSQLI_ASSOC);

mysqli_close($con);

?>

PHP mysqli_fetch_array() Function

The PHP MySQLi "mysqli_fetch_array" function fetches rows as an associative or numeric array or both and this function returns an array of strings that corresponds to the fetched row or null if there are no more rows in the resultset.

Syntax

mysqli_fetch_array(result, resulttype)

Parameters of the mysqli_fetch_array function

The parameters of the function are: 

Parameter Description
result It specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_user_result().
resulttype It specifies what type of array that should be produce. Can be one of the following values:
  • MySQLI_ASSOC
  • MYSQLI_NUM
  • MYSQLI_BOTH

Example

An example of the function is:

<?php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($con))
  {
  echo "
Failed to connect to MySQL: " . mysqli_connect_error();
  }
print "<
h2>MySQL: Data of Emp table</h2>";
$result =mysqli_query($con,"SELECT * FROM emp");
echo "
<table border='1'>
<
tr>
<
th>Id</th>
<
th>Name</th>
<
th>Salary</th>
</
tr>";
while($rowval = mysqli_fetch_array($result))
  {
  echo "
<tr>";
  echo "
<td>" . $rowval['id'] . "</td>";
  echo "
<td>" . $rowval['name'] . "</td>";
  echo "
<td>" . $rowval['salary'] . "</td>";
  echo "
</tr>";
  }echo "
</table>";
/*........For Fetch data of Emp Table
<br>
  .......*/
mysqli_close($con);

?>


Output

mysql-fetch-array-function-in-php.jpg 

PHP mysqli_fetch_assoc() Function

The PHP MySQLi "mysqli_fetch_assoc" function fetches a row as an associative array and this function returns an associative array of strings representing the fetched row in the result set.

Syntax

mysqli_fetch_assoc(result)

Parameters of the mysqli_fetch_assoc function

The parameter of the function is: 

Parameter Description
result It specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_user_result().

Example

An example of the function is:

<?php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($con))
  {
  echo "
Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result =mysqli_query($con,"
SELECT * FROM emp");
$row=mysqli_fetch_assoc($result);
printf ("
%s (%s)\n",$row["id"],$row["name"]);
?>


Output

mysqli-fetch-assoc-function-in-php.jpg 

PHP mysqli_fetch_field_direct() Function

The PHP MySQLi "mysqli_fetch_field_direct" function fetches meta data for a single field and this function returns an object that contains the field-definition information or false if no field information is specified.


Syntax

mysqli_fetch_field_direct(result,fieldnr)

Parameters of the mysqli_fetch_field_direct function

The parameters of the function are: 

Parameter Description
result It specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_user_result().
fieldnr It specifies the field number.

Example

An example of the function is:


<?
php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($con))
  {
  echo "
Failed to connect to MySQL: " . mysqli_connect_error();
  }
if($result =mysqli_query($con,"
SELECT * FROM emp"))
{
$info=mysqli_fetch_field_direct($result,1);
printf("<
b>Emp Name: </b>%s",$info->name);
echo "
<br>";
printf("
<b>Table Name: </b>%s",$info->table);
echo "
<br>";
printf("
<b>max. Len:</b> %d\n",$info->max_length);
}
mysqli_close($con);
?>

Output

mysqli-fetch-field-direct-function-in-php.jpg

PHP mysqli_fetch_field() Function

The PHP MySQLi "mysqli_fetch_field" function returns the next field in the result set, in other words this function returns an object that contains the field definition information or false if no field information is available.

Syntax

mysqli_fetch_field(result)

Parameters of the mysqli_fetch_field function

The parameter of the function is:

Parameter Description
result It specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_user_result().

Example

An example of the function is:


<?
php
$
con=mysqli_connect("localhost","root","","mysql");
if (mysqli_connect_errno($con))
  {
  echo "
Failed to connect to MySQL: " . mysqli_connect_error();
  }
if($result =mysqli_query($con,"
SELECT * FROM emp"))
{
$info=mysqli_fetch_field($result);
printf("<
b>Emp Name: </b>%s",$info->name);
echo "
<br>";
printf("
<b>Table Name: </b>%s",$info->table);
echo "
<br>";
printf("
<b>max. Len:</b> %d\n",$info->max_length);
}
mysqli_close($con);

?>
 

Output

mysqli-fetch-field-function-in-php.jpg