PHP MySQLi 函数

PHP mysqli_result::$current_field / mysqli_field_tell() 函数返回最后一个字段光标的位置mysqli_fetch_field() 调用。该值可用作 mysqli_field_seek() 的参数。

语法

//面向对象风格
$mysqli_result->current_field;

//面向过程风格
mysqli_field_tell(result)

参数

结果 必填。 仅适用于面向过程风格:指定 mysqli_query()、mysqli_store_result()、mysqli_use_result() 或 mysqli_stmt_get_result() 返回的 mysqli_result 对象。

返回值

返回字段光标的当前偏移量。

示例:面向对象风格

下面的示例显示mysqli_result::$ 的用法current_field 属性。

<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//从数据库获取查询结果
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";

if ($result = $mysqli->query($sql)) {

  //获取所有列的字段信息
  while ($finfo = $result->fetch_field()) {

    //获取字段指针偏移量
    $currentfield = $result->current_field;

    printf("Column %d:\n", $currentfield);
    printf("Name:     %s\n", $finfo->name);
    printf("Table:    %s\n", $finfo->table);
    printf("max. Len: %d\n\n", $finfo->max_length);
  }

  //释放与结果相关的内存
  $result->close();
}

//关闭连接
$mysqli->close();
?>

上述代码的输出将类似于:

Column 1:
Name:     Name
Table:    Employee
max. Len: 50

Column 2:
Name:     Age
Table:    Employee
max. Len: 10

示例:面向过程风格

下面的示例显示了 mysqli_field_tell() 函数的用法。

<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//从数据库获取查询结果
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";

if ($result = mysqli_query($mysqli, $sql)) {

  //获取所有列的字段信息
  while ($finfo = mysqli_fetch_field($result)) {

    //获取字段指针偏移量
    $currentfield = mysqli_field_tell($result);

    printf("Column %d:\n", $currentfield);
    printf("Name:     %s\n", $finfo->name);
    printf("Table:    %s\n", $finfo->table);
    printf("max. Len: %d\n\n", $finfo->max_length);
  }

  //释放与结果相关的内存
  mysqli_free_result($result);
}

//关闭连接
mysqli_close($mysqli);
?>

上述代码的输出将类似于:

Column 1:
Name:     Name
Table:    Employee
max. Len: 50

Column 2:
Name:     Age
Table:    Employee
max. Len: 10