• PHP Video Tutorials

PHP mysqli_fetch_lengths() Function



Definition and Usage

A PHP result object (of the class mysqli_result) represents the MySQL result, returned by the SELECT or, DESCRIBE or, EXPLAIN queries.

The mysqli_fetch_lengths() function accepts a result object as a parameter, retrieves the lengths of columns in the current row of the given result, and returns them in the form of an array.

Syntax

mysqli_fetch_lengths($result);

Parameters

Sr.No Parameter & Description
1

result(Mandatory)

This is an identifier representing a result object.

Return Values

The PHP mysqli_fetch_lengths() function returns an array (of integers) containing the length/size of each column (of the current row), if successful and, it returns the boolean value FALSE in case of an error.

PHP Version

This function was first introduced in PHP Version 5 and works works in all the later versions.

Example

Following example demonstrates the usage of the mysqli_fetch_lengths() function (in procedural style) −

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   print("Record Inserted.....\n");

   //Retrieving the contents of the table
   $res = mysqli_query($con, "SELECT * FROM myplayers");

   //Lengths of the rows
   $row = mysqli_fetch_row($res);
   $lengths = mysqli_fetch_lengths($res);

   print_r($lengths);
   mysqli_free_result($res);

   mysqli_close($con);
?>

This will produce following result −

Table Created.....
Record Inserted.....
Array
(
    [0] => 1
    [1] => 6
    [2] => 6
    [3] => 5
    [4] => 5
)

Example

In object oriented style the syntax of this function is $result->lengths; Following is the example of this function in object oriented style $minus;

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");
  
   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Table Created.....\n");

   $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';

   //Executing the statement
   $stmt->execute();

   //Retrieving the result
   $res = $stmt->get_result();
   $row = $res->fetch_row();

   $len = $res ->lengths;
   print_r($len);

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

This will produce following result −

Table Created.....
Array
(
    [0] => 4
    [1] => 0
)

Example

For this function to retrieve the lengths, it is mandatory to call it after fetching the rows using mysqli_fetch_row/array/object methods otherwise, it returns false $minus;

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE data(ID INT, Name VARCHAR(255), Age INT)");
   mysqli_query($con, "INSERT INTO data values(1, 'Mohan', 25)");
   mysqli_query($con, "INSERT INTO data values(2, 'Syamala', 36)");
   print("Record Inserted.....\n");

   //Retrieving the contents of the table
   $res = mysqli_query($con, "SELECT * FROM data");

   //Lengths of the rows
   $bool = $lengths = mysqli_fetch_lengths($res);

   if($bool){
      print("Lengths Retrieved");
   }else{
      print("Failed");
   }

   print_r($lengths);
   mysqli_free_result($res);

   mysqli_close($con);
?>

This will produce following result −

Record Inserted.....
Failed
php_function_reference.htm
Advertisements