PHP lengths / mysqli_fetch_lengths() 函数
示例 - 面向对象的样式
返回结果集中的字段长度:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$sql = "SELECT * FROM Persons ORDER BY Lastname";
if ($result = $mysqli -> query($sql)) {
$row = $result -> fetch_row();
// Display field lengths
foreach ($result -> lengths as $i => $val) {
printf("Field %2d has length: %2d\n", $i + 1, $val);
}
$result -> free_result();
}
$mysqli -> close();
?>
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$sql = "SELECT * FROM Persons ORDER BY Lastname";
if ($result = $mysqli -> query($sql)) {
$row = $result -> fetch_row();
// Display field lengths
foreach ($result -> lengths as $i => $val) {
printf("Field %2d has length: %2d\n", $i + 1, $val);
}
$result -> free_result();
}
$mysqli -> close();
?>
查看底部的程序样式示例。
定义和用法
lengths / mysqli_fetch_lengths() 函数返回结果集中的字段长度。
语法
面向对象的风格:
$mysqli_result -> lengths
程序风格:
mysqli_fetch_lengths(result)
参数值
参数 | 描述 |
---|---|
result | 必需。规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符。 |
技术细节
返回值: | 如果整数表示每个字段(列)的长度则返回数组,如果发生错误则返回 FALSE。 |
---|---|
PHP 版本: | 5+ |
示例 - 程序样式
返回结果集中的字段长度:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql = "SELECT * FROM Persons ORDER BY Lastname";
if ($result = mysqli_query($con, $sql)) {
$row = mysqli_fetch_row($result);
// Display field lengths
foreach (mysqli_fetch_lengths($result) as $i => $val) {
printf("Field %2d has length: %2d\n", $i+1, $val);
}
mysqli_free_result($result);
}
mysqli_close($con);
?>
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql = "SELECT * FROM Persons ORDER BY Lastname";
if ($result = mysqli_query($con, $sql)) {
$row = mysqli_fetch_row($result);
// Display field lengths
foreach (mysqli_fetch_lengths($result) as $i => $val) {
printf("Field %2d has length: %2d\n", $i+1, $val);
}
mysqli_free_result($result);
}
mysqli_close($con);
?>