目录

PHP lengths / mysqli_fetch_lengths() 函数

❮ PHP MySQLi 参考

示例 - 面向对象风格

返回结果集中当前行的字段长度:

<?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();
?>

查看底部的程序样式示例。


定义和用法

lengths / mysqli_fetch_lengths() 函数返回结果集中当前行的字段长度。


语法

面向对象风格:

$mysqli_result -> lengths

程序风格:

mysqli_fetch_lengths( result)

参数值

Parameter Description
result Required. Specifies a result set identifier returned by mysqli_query(), mysqli_store_result() or 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);
?>


❮ PHP MySQLi 参考