(PHP 5, PHP 7, PHP 8)
mysqli_result::$lengths -- mysqli_fetch_lengths — 返回结果集当前行中所有列的长度
面向对象风格
过程式风格
函数 mysqli_fetch_lengths() 返回一个数组,其中包含结果集中当前行中所有列的长度。
result
仅限过程式风格:一个由 mysqli_query()、mysqli_store_result()、mysqli_use_result() 或 mysqli_stmt_get_result() 返回的 mysqli_result 对象。
一个整数数组,表示每列的大小(不包括任何终止的 null 字符)。如果发生错误,则返回 false
。
mysqli_fetch_lengths() 仅对结果集的当前行有效。如果您在调用 mysqli_fetch_row/array/object 之前或在检索结果集中的所有行之后调用它,则它将返回 false
。
示例 #1 面向对象风格
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("连接失败:%s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * from Country ORDER BY Code LIMIT 1";
if ($result = $mysqli->query($query)) {
$row = $result->fetch_row();
/* 显示列长度 */
foreach ($result->lengths as $i => $val) {
printf("字段 %2d 的长度为 %2d\n", $i+1, $val);
}
$result->close();
}
/* 关闭连接 */
$mysqli->close();
?>
示例 #2 过程式风格
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("连接失败:%s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * from Country ORDER BY Code LIMIT 1";
if ($result = mysqli_query($link, $query)) {
$row = mysqli_fetch_row($result);
/* 显示列长度 */
foreach (mysqli_fetch_lengths($result) as $i => $val) {
printf("字段 %2d 的长度为 %2d\n", $i+1, $val);
}
mysqli_free_result($result);
}
/* 关闭连接 */
mysqli_close($link);
?>
上面的示例将输出
Field 1 has Length 3 Field 2 has Length 5 Field 3 has Length 13 Field 4 has Length 9 Field 5 has Length 6 Field 6 has Length 1 Field 7 has Length 6 Field 8 has Length 4 Field 9 has Length 6 Field 10 has Length 6 Field 11 has Length 5 Field 12 has Length 44 Field 13 has Length 7 Field 14 has Length 3 Field 15 has Length 2