PHP mysqli_result::$lengths / mysqli_fetch_lengths() 函数返回一个数组,其中包含结果集中的当前行。
语法
//面向对象风格
$mysqli_result->lengths;
//面向过程风格
mysqli_fetch_lengths(result)
参数
结果 | 必填。 仅适用于面向过程风格:指定 mysqli_query()、mysqli_store_result()、mysqli_use_result() 或 mysqli_stmt_get_result() 返回的 mysqli_result 对象。 |
返回值
返回表示每列大小的整数数组(不包括任何终止空字符)。如果发生错误,则返回 false。
mysqli_fetch_lengths() 仅对结果集的当前行有效。如果在调用 mysqli_fetch_row/array/object 之前或检索结果中的所有行之后调用,则返回 false。
示例:面向对象风格
下面的示例显示了 的用法mysqli_result::$lengths 属性。
<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: ". $mysqli->connect_error;
exit();
}
//从数据库获取查询结果
$sql = "SELECT EmpId, Name, Age FROM Employee ORDER BY Age";
if ($result = $mysqli->query($sql)) {
//从结果集中取出一行数据
$row = $result->fetch_row();
//显示列长度
foreach ($result->lengths as $i => $val) {
printf("Field %2d has Length %2d\n", $i+1, $val);
}
//释放与结果相关的内存
$result->close();
}
//关闭连接
$mysqli->close();
?>
上述代码的输出将类似于:
Field 1 has Length 4
Field 2 has Length 12
Field 3 has Length 3
示例:面向过程风格
下面的示例显示了 mysqli_fetch_lengths() 函数的用法。
<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ". mysqli_connect_error();
exit();
}
//从数据库获取查询结果
$sql = "SELECT EmpId, Name, Age FROM Employee ORDER BY Age";
if ($result = mysqli_query($mysqli, $sql)) {
//从结果集中取出一行数据
$row = mysqli_fetch_row($result);
//显示列长度
foreach (mysqli_fetch_lengths($result) as $i => $val) {
printf("Field %2d has Length %2d\n", $i+1, $val);
}
//释放与结果相关的内存
mysqli_free_result($result);
}
//关闭连接
mysqli_close($mysqli);
?>
上述代码的输出将类似于:
Field 1 has Length 4
Field 2 has Length 12
Field 3 has Length 3