Current Location: Home> Function Categories> mysqli_result::$lengths

mysqli_result::$lengths

(mysqli_fetch_lengths) Returns the column length of the current row in the result set
Name:mysqli_result::$lengths
Category:MySQLi
Programming Language:php
One-line Description:Get the length of each field in the result set

Function name: mysqli_result::$lengths()

Applicable version: PHP 5 >= 5.3.0, PHP 7

Function description: mysqli_result::$lengths() function is used to get the length of each field in the result set.

Syntax: array mysqli_result::lengths ( void )

Parameter description: No parameters need to be passed in.

Return Value: Returns an array containing the length of each field in the result set. If the result set is empty, the return value is NULL.

Sample code:

 // 假设已经连接到数据库,并执行了查询语句$query = "SELECT * FROM users"; $result = $mysqli->query($query); // 检查查询是否成功if ($result) { // 获取每个字段的长度$lengths = $result->lengths(); // 遍历结果集中的每一行while ($row = $result->fetch_assoc()) { // 遍历每一行的每个字段foreach ($row as $key => $value) { // 输出字段值及其长度echo "字段" . $key . " 的值为" . $value . ",长度为" . $lengths[$key] . "<br>"; } echo "<br>"; } // 释放结果集$result->free(); } else { echo "查询失败!"; }

In the example above, we first execute a query statement and save the result in the $result variable. We then use the mysqli_result::$lengths() function to get the length of each field in the result set and save it in the $lengths variable. Next, we use the fetch_assoc() function to iterate through each row in the result set and use foreach to loop through each field, outputting the value and length of the field. Finally, we release the result set.

Note that before using the mysqli_result::$lengths() function, you must call mysqli_result::fetch_assoc() or other similar functions to get a row of data, otherwise the length of the field will not be obtained.

Similar Functions
Popular Articles