MySQLi Function in PHP: Part 5

Introduction

In this article I describe the PHP MySQLi  functions mysqli_fetch_fields, mysqli_fetch_lengths, mysqli_fetch_object, mysqli_fetch_row and mysqli-field_count. 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
  4. MySQLi Function in PHP: Part 4

PHP mysqli_fetch_fields() Function

The PHP MySQLi "mysqli_fetch_fields" function returns an array of objects representing the fields in a result set or you can say that this function returns an array of objects that contains information or returns false if no field information is available.

Syntax

mysqli_fetch_fields(result, resulttype)

Parameter in mysqli_fetch_fields 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"))
{
$fieldsinfo=mysqli_fetch_fields($result);
foreach ($fieldsinfo as $val)
    {
    printf("<
b>Name:</b> %s",$val->name);
    printf(" 
<b>Table:</b> %s",$val->table);
    printf(" 
<b>max. Len:</b> %d",$val->max_length);
       echo "
</br>";
    }
}
mysqli_close($con);

?> 


Output

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

PHP mysqli_fetch_lengths() Function

The PHP MySQLi "mysqli_fetch_lengths function" gets the length of each output in a result and this function returns an array of lengths on success or false on failure.

Syntax

mysqli_fetch_lengths(result, resulttype)

Parameter in mysqli_fetch_lengths 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();

  }

$qry=mysqli_query($con,"SELECT * FROM emp");

$row=mysqli_fetch_assoc($qry);

$lengths=mysqli_fetch_lengths($qry);

echo "<pre>";

print_r($row);

echo "<pre>";

echo "</br>";

print_r($lengths);

mysqli_close($con);

?> 

Output

mysqli-fetch-lengths-in-php.jpg

PHP mysqli_fetch_object() Function

The PHP MySQLi "mysqli_fetch_objects" function returns the current row of a result set as an object or in other words this function returns an object with string properties that corresponds to the fetched row or null if there are no more rows in the result set.

Syntax

mysqli_fetch_object(result,classname,params)

Parameter in mysqli_fetch_object 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().
classname It specifies the name of the class to instantiate and return. If not specified a class object is returned.
params It specifies an array of parameters to pass to the constructor for classname objects.

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();
  }
echo "<
table border='1'>
<
tr>
<
th>EmpId</th>
<
th>EmpName</th>
</
tr>";
if($qry=mysqli_query($con,"SELECT * FROM emp"))
{
while($res=mysqli_fetch_object($qry))
{
echo "
<tr>";
echo "
<td>" . $res->id . "</td>";
echo "
<td>" . $res->name . "</td>";
echo "
</tr>";
}
}
echo "
</table>";
mysqli_close($con);

?> 


Output

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

PHP mysqli_fetch_row() Function

The PHP MySQLi "mysqli_fetch_lengths" function gets a result row as an enumerated array and this function returns an array of strings that corresponds to the fetched row or null if there are no more rows in the result set.

Syntax

mysqli_fetch_row(result)

Parameter in mysqli_fetch_row 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();
  }
echo "<
table border='1'>
<
tr>
<
th>EmpId</th>
<
th>EmpName</th>
<
th>EmpSalary</th>
</
tr>";
 
if($qry=mysqli_query($con,"SELECT * FROM emp"))
{
while($res=mysqli_fetch_row($qry))
{
echo "
<tr>";
echo "
<td>" . $res[0] . "</td>";
echo "
<td>" . $res[1] . "</td>";
echo "
<td>" . $res[2] . "</td>";
echo "
</tr>";
}
}
echo "
</table>";
mysqli_close($con);

?>

 
Output

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

PHP mysqli_field_count() Function

The PHP MySQLi "mysqli_field_count" function returns the number of columns for the most recent query and this function returns an integer representing the number of fields in a result set.

Syntax

mysqli_field_count(connection)

Parameter in mysqli_field_count function

The parameter of the function is: 

Parameter Description
connection It specifies the MySQL connection to use.

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();
}
mysqli_query($con,"
SELECT * FROM emp");
echo "
Total no of fields is: ".mysqli_field_count($con);
mysqli_close($con);

?>


Output

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

Up Next
    Ebook Download
    View all
    Learn
    View all