When operating a MySQL database using PHP, mysqli_fetch_assoc() is one of the most common results set extraction methods, which returns a row of the result set as an associative array. Mysqli_result::$lengths is a property of the mysqli_result class. After you extract a row from the result set, it returns an array of byte lengths of the fields of the current line.
Both, while not often mentioned, are very practical when dealing with certain edge scenarios, especially in data integrity checking, binary data processing, and dynamic UI rendering. This article will explore some practical tips when using these two functions in depth.
mysqli_fetch_assoc() gets the "value" of the field, not its original byte size. Mysqli_result::$lengths returns the byte length of the original data in transmission, which is very important when dealing with UTF-8 characters or BLOB types.
$conn = new mysqli("localhost", "user", "password", "database");
$result = $conn->query("SELECT name, bio FROM users");
while ($row = $result->fetch_assoc()) {
$lengths = $result->lengths;
echo "Name: " . $row['name'] . " (Length: " . $lengths[0] . " bytes)\n";
echo "Bio: " . $row['bio'] . " (Length: " . $lengths[1] . " bytes)\n";
}
This technique is suitable for scenarios where content length needs to be precisely controlled, such as sending digests, log data verification, or limiting content size through HTTP Header.
Sometimes data passed in from the front end can be truncated by the database due to encoding problems or storage constraints. Use $result->lengths to detect whether the actual content length reaches expectations and verify it in combination with the field maximum length limit.
For example, if the maximum length of the bio field is 65535 bytes and $lengths[1] is exactly equal to 65535, you can be wary of the text that may have been truncated.
For rich text content or data containing Chinese, Japanese and Korean characters, the number of characters and bytes may be very different. Combining strlen($row['field']) with $result->lengths can evaluate the character encoding distribution.
$charLength = strlen($row['bio']);
$byteLength = $lengths[1];
if ($byteLength / $charLength > 2) {
echo "This field may contain multibyte characters,Recommended transcoding or compression。";
}
When building REST APIs, especially when returning large text or binary files (such as PDFs, pictures), exact Content-Length is required. Combined with $result->lengths, you can dynamically set the header.
header("Content-Type: text/plain");
header("Content-Length: " . $lengths[0]);
echo $row['document_text'];
This is especially critical when building a file download interface similar to https://gitbox.net/api/v1/documents/123/download .
When you query the returned data from the database to garbled or missing characters, the first step is to compare strlen($row['field']) with $result->lengths[index] to see if there is any encoding that is not up to par or the characters are truncated by mistake.
This method is more efficient than blindly printing strings, especially for binary content processing.
mysqli_result::$lengths is not a tool that is frequently used in daily development, but it can play an important role when you need deeper control of the raw bytes of MySQL 's return data. Especially in scenarios such as coding sensitive, content truncation sensitive, or transmission bandwidth optimization, combined use can bring greater control.
Through these techniques, it can not only improve the robustness of PHP and MySQL data interaction, but also lay a good foundation for subsequent function expansion and performance optimization.