PHP mysqli_result class

This article is created to list properties and methods of mysqli_result class, along with brief description and example.

PHP mysqli_result Class Properties and Description

Property Description
current_field Used to get the current field offset of result pointer
field_count Used to get the number of fields (columns) available in the result set
lengths Used to get the lengths of columns of current row in the result set
num_rows Used to get the number of rows

PHP mysqli_result Class Methods and Description

Method Description
__construct() Used to construct a mysqli_result object
data_seek() Used to move the result pointer to an arbitrary row in the result set
fetch_all() Used to fetch all result row as an associative array or as a numeric array or as both
fetch_array() Used to fetch the next row, as an associative array or as a numeric array or as both
fetch_assoc() Used to fetch the next row as an associative array
fetch_column() Used to fetch a single column from the next row
fetch_field_direct() Used to fetch metadata (data about data) for a single field (column)
fetch_field() Used to get the next field (column)
fetch_fields() Used to get an array of objects representing the fields
fetch_object() Used to fetch next row as an object
fetch_row() Used to fetch next row as an enumerated array
field_seek() Used to set the result pointer to a specified field offset
free() Used to free the memory that is associated with the result set
getIterator() Used to retrieve an external iterator

PHP mysqli_result Class Example

Before creating an example of mysqli_result class, let's see the table, that are going to use in the example:

php mysqli result class example table

PHP mysqli_result Class Properties Example

This examples uses the two famous and most used properties of the mysqli_result class in PHP:

<?php
   $conn = new mysqli("localhost", "root", "", "codescracker");
   
   if(!$conn->connect_errno)
   {
      $stmt = $conn->prepare("SELECT * FROM customer");
      $stmt->execute();
      $result = $stmt->get_result();
      
      echo "<h2>Using field_count Property</h2>";
      echo $result->field_count;
      echo "<HR>";
      
      echo "<h2>Using num_rows Property</h2>";
      echo $result->num_rows;
      echo "<HR>";
   }
   $conn->close();
?>

The output is:

php mysqli result class properties

PHP mysqli_result Class Methods Example

This examples uses some of the properties of PHP mysqli_result class:

<?php
   $conn = new mysqli("localhost", "root", "", "codescracker");
   
   if(!$conn->connect_errno)
   {
      $stmt = $conn->prepare("SELECT * FROM customer");
      $stmt->execute();
      $result = $stmt->get_result();
      
      echo "<h2>Using fetch_all() Method</h2>";
      print_r($result->fetch_all());
      echo "<HR>";
      
      echo "<h2>Using fetch_field() Method</h2>";
      print_r($result->fetch_field());
      echo "<HR>";
      
      echo "<h2>Using fetch_fields() Method</h2>";
      print_r($result->fetch_fields());
      echo "<HR>";
   }
   $conn->close();
?>

The output of above example on mysqli_result class, based on my current customer table, should be:

Using fetch_all() Method
Array ( [0] => Array ( [0] => 1 [1] => Olivia [2] => 28 [3] => codescracker.com@gmail.com ) [1] => Array ( [0] => 2 [1] => Charlotte [2] => 24 [3] => charloette@xyz.com ) [2] => Array ( [0] => 4 [1] => Sophia [2] => 29 [3] => sophia@xyz.com ) [3] => Array ( [0] => 5 [1] => Benjamin [2] => 31 [3] => benjamin@xyz.com ) [4] => Array ( [0] => 6 [1] => Susan [2] => 35 [3] => susan@xyz.com ) [5] => Array ( [0] => 7 [1] => Martin [2] => 35 [3] => martin@xyz.com ) )

Using fetch_field() Method stdClass Object ( [name] => id [orgname] => id [table] => customer [orgtable] => customer [def] => [db] => codescracker [catalog] => def [max_length] => 0 [length] => 6 [charsetnr] => 63 [flags] => 49699 [type] => 3 [decimals] => 0 )
Using fetch_fields() Method Array ( [0] => stdClass Object ( [name] => id [orgname] => id [table] => customer [orgtable] => customer [def] => [db] => codescracker [catalog] => def [max_length] => 0 [length] => 6 [charsetnr] => 63 [flags] => 49699 [type] => 3 [decimals] => 0 ) [1] => stdClass Object ( [name] => name [orgname] => name [table] => customer [orgtable] => customer [def] => [db] => codescracker [catalog] => def [max_length] => 0 [length] => 120 [charsetnr] => 45 [flags] => 4097 [type] => 253 [decimals] => 0 ) [2] => stdClass Object ( [name] => age [orgname] => age [table] => customer [orgtable] => customer [def] => [db] => codescracker [catalog] => def [max_length] => 0 [length] => 2 [charsetnr] => 63 [flags] => 32768 [type] => 3 [decimals] => 0 ) [3] => stdClass Object ( [name] => email [orgname] => email [table] => customer [orgtable] => customer [def] => [db] => codescracker [catalog] => def [max_length] => 0 [length] => 160 [charsetnr] => 45 [flags] => 4097 [type] => 253 [decimals] => 0 ) )

PHP Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube