fetch_lengths
In this article, we will focus on the mysqli_fetch_lengths() function in PHP, which is used to fetch an array of the lengths of the columns in a result set. We
In this article, we will focus on the mysqli_fetch_lengths() function in PHP. It returns an array containing the lengths of each column in the current row of a MySQLi result set. We will explain its syntax, return values, the object-oriented equivalent, common use cases, and the gotchas to watch for.
What mysqli_fetch_lengths() does
The mysqli_fetch_lengths() function is a built-in PHP function that returns the lengths — in bytes — of every column value in the row that was most recently fetched from a MySQLi result set. It does not read the row itself; instead, it reports the byte length of each column in the row the result pointer currently sits on.
This matters because MySQL stores everything it sends to the client as bytes, and the length is the actual size of the data returned — not the column's defined maximum size. For example, a VARCHAR(255) column holding the string "hello" reports a length of 5, not 255. For multibyte (UTF-8) text, the length is the number of bytes, which can be larger than the number of characters.
Syntax
mysqli_fetch_lengths(mysqli_result $result): array|false$result— a result object returned bymysqli_query(),mysqli_store_result(), ormysqli_use_result().- Returns: a numerically indexed array of column lengths for the current row, or
falseif no row has been fetched yet (or on error).
When to use it
You reach for mysqli_fetch_lengths() when the size of the returned data matters as much as the data itself:
- Handling binary / BLOB columns where you need the exact byte count before processing.
- Defensive checks — confirming a column actually returned data versus an empty string.
- Logging, debugging, or diagnostics about how much data each query returns.
Because it depends on the row pointer, you must call it after a fetch (mysqli_fetch_row(), mysqli_fetch_array(), or mysqli_fetch_assoc()) and before the next fetch advances the pointer.
How to use the mysqli_fetch_lengths() function
mysqli_fetch_lengths() operates on the current row pointer, so you typically call it inside a fetch loop, right after fetching a row:
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);
if ($result) {
while ($row = mysqli_fetch_row($result)) {
$lengths = mysqli_fetch_lengths($result);
if ($lengths !== false) {
for ($i = 0; $i < count($lengths); $i++) {
printf("Length of column %d: %d\n", $i, $lengths[$i]);
}
}
}
}
mysqli_close($mysqli);
?>In this example, we connect to the database and execute a query. We check that the connection and query succeeded. Inside the while loop, mysqli_fetch_row() advances the row pointer to the next row. We then call mysqli_fetch_lengths() to get an array of column lengths for that row. The function returns false on failure, so we verify the result before looping through the lengths with a for loop to print them.
Object-oriented style
The same operation is available as a method on the mysqli_result object. Most modern code uses this style:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
}
$result = $mysqli->query("SELECT id, name FROM my_table");
if ($result) {
while ($row = $result->fetch_row()) {
$lengths = $result->lengths; // property, not a method call
printf("id length: %d, name length: %d\n", $lengths[0], $lengths[1]);
}
}
$mysqli->close();
?>Note that in OOP style the lengths are exposed as the read-only $result->lengths property, not a method.
Common gotchas
- Call it after a fetch, not before. If you call
mysqli_fetch_lengths()before any row has been fetched, it returnsfalse— the row pointer has nothing to measure. - Lengths are in bytes. For multibyte text (such as UTF-8), the byte length can exceed the character count. Use
mb_strlen()if you need a character count instead. - It reflects only the current row. Calling it again after fetching the next row gives the lengths for that new row, so store the array if you need the previous values.
NULLcolumns report a length of0, the same as an empty string — combine it with the fetched value if you need to distinguish them.
Related functions
mysqli_fetch_row()— fetch a row as a numeric array (commonly paired with this function).mysqli_fetch_array()andmysqli_fetch_assoc()— fetch a row as a numeric/associative or associative array.mysqli_fetch_fields()— get metadata (name, type, max length) about each column.mysqli_field_count()— get the number of columns in the last query.
Conclusion
The mysqli_fetch_lengths() function is a practical tool for retrieving the byte size of each column in the current row of a MySQLi result set. Remember that it operates on the most recently fetched row, returns lengths in bytes, and yields false when no row is available — keeping those rules in mind lets you integrate it reliably into your database workflows.