English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_fetch_lengths() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_fetch_lengths() function returns the field lengths in the result set.

Definition and Usage

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

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

Syntax

mysqli_fetch_lengths($result);

Parameters

Serial numberParameters and descriptions
1

result (required)

This is the identifier representing the result object.

Return value

The PHP mysqli_fetch_lengths() function returns an array (integer), which contains the length of each column (for the current row)/size (if successful), and returns a boolean value in case of an errorFALSE.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following example demonstratesmysqli_fetch_lengths()Usage of the function (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("Create table.....\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("Inserting record.....\n");
   //Retrieve table content
   $res = mysqli_query($con, "SELECT * FROM myplayers");
   //Row length
   $row = mysqli_fetch_row($res);
   $lengths = mysqli_fetch_lengths($res);
   print_r($lengths);
   mysqli_free_result($res);
   mysqli_close($con);
?>

Output result

Create table.....
Inserting record.....
Array
(
    [0] => 1
    [1] => 6
    [2] => 6
    [3] => 5
    [4] => 5
)

Online Example

In an object-oriented style, the syntax of this function is$result-> lengths;.Here is an example of this function in an object-oriented style;

<?php
   //Establish 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("Create table.....\n");
   $stmt = $con -> prepare("SELECT}} * FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   //Execute statement
   $stmt->execute();
   //Retrieve result
   $res = $stmt->get_result();
   $row = $res->fetch_row();
   $len = $res ->lengths;
   print_r($len);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....
Array
(
    [0] => 4
    [1] => 0
)

Online Example

This function must be called after using mysqli_fetch_row or mysqli_fetch_array or mysqli_fetch_object methods to read rows, otherwise it returns false:

<?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("Inserting record.....\n");
   //Retrieve table content
   $res = mysqli_query($con, "SELECT * FROM data);
   //Row length
   $bool = $lengths = mysqli_fetch_lengths($res);
   if($bool){
      print("Found lengths");
   }else{
      print("Failed");
   }
   print_r($lengths);
   mysqli_free_result($res);
   mysqli_close($con);
?>

Output result

Inserting record.....
Failed
Test and see‹/›

PHP MySQLi Reference Manual