PHP mysqli_fetch_lengths() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_fetch_lengths() returns an array of byte lengths for each column in the current row of a mysqli_result.

Call it after a successful row fetch such as mysqli_fetch_row() or mysqli_fetch_assoc(). If no row is current it returns false. The object style is $result->lengths (property) or related result APIs; procedurally use mysqli_fetch_lengths().

Syntax

Call mysqli_fetch_lengths() with the values you need. Start with this line.

PHP
$lengths = mysqli_fetch_lengths($result);
CallWhat it does
mysqli_fetch_row($result); mysqli_fetch_lengths($result)Fetches a row, then returns the byte length of each column value.
$lengths[0]Reads the length of the first column in the current row.
mysqli_fetch_lengths($result)Returns false if you call it before any row has been fetched.

Lengths are for the row you most recently fetched. NULL SQL values typically report length 0. This is a mysqli_result API, so you pass the result—not the connection—to the procedural function.

Library declarations
PHP
mysqli_fetch_lengths(mysqli_result $result): array|false

Parameters

ParameterDescription
resultRequired. A mysqli_result that currently points at a fetched row.

Return Value

Return ValueDescription
array|falseAn array of integers (one length per column), or false when no current row is available.

PHP Examples

Example 1 — Measure Columns of the Current Row

Fetch one row, then print each column length from a fixed sample.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products WHERE id = 1");
mysqli_fetch_row($result);
$lengths = mysqli_fetch_lengths($result);

echo implode(", ", $lengths), PHP_EOL;

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. mysqli_fetch_row() loads the current row into the result.
  2. mysqli_fetch_lengths() returns one integer per column.
  3. In the sample, id has length 1 and name "Tea Mug" has length 7.
  4. Call lengths only after a successful row fetch.

Example 2 — False Before Any Fetch

Calling mysqli_fetch_lengths() with no current row returns false.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products LIMIT 1");
$lengths = mysqli_fetch_lengths($result);

var_dump($lengths);

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. A result exists, but no row has been fetched yet.
  2. mysqli_fetch_lengths() has no current row to measure.
  3. The function returns false.
  4. Fetch a row first, then call it again.

Example 3 — Lengths Inside a Fetch Loop

For each associative row, print the name and its byte length from sample data.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT name FROM products ORDER BY id LIMIT 2");

while ($row = mysqli_fetch_assoc($result)) {
    $lengths = mysqli_fetch_lengths($result);
    echo $row["name"], " => ", $lengths[0], PHP_EOL;
}

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. Each loop fetches the next row.
  2. mysqli_fetch_lengths() then reports lengths for that row.
  3. Index 0 matches the single name column.
  4. Sample lengths follow the demo product names.

Frequently Asked Questions

After you fetch a row, it returns an array of byte lengths for each column value in that current row.
Usually because no row is current yet. Fetch with mysqli_fetch_row() or mysqli_fetch_assoc() first.
It is a mysqli_result API. Pass the result object to the procedural function.
Read the lengths property on the result after fetching a row, for example $result->lengths.
They are byte lengths. Multibyte text can use more bytes than visible characters.

Next: mysqli_fetch_object()

mysqli_fetch_object() reads the next row from a mysqli_result and returns it as an object .

mysqli_fetch_object() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful