PHP mysqli_field_tell() 函数用法及示例 - PHP教程

由网友 大卫 发布 阅读 2

PHP mysqli_field_tell()  函数用法及示例 - PHP教程

PHP MySQLi 参考手册

mysqli_field_tell()函数返回字段指针的位置。

定义和用法

一个PHP结果对象(类mysqli_result)表示由SELECT或DESCRIBE或EXPLAIN查询返回的MySQL结果。结果对象的字段光标/指针指向其中的字段(列值)。

mysqli_field_tell()函数接受一个结果对象作为参数,检索和返回在给定对象中的字段指针的当前位置。

语法

mysqli_field_tell($result);

参数

序号参数及说明
1

result(必需)

这是表示结果对象的标识符。

返回值

PHP mysqli_field_tell()函数返回一个整数值,该值指定字段指针在给定结果对象中的当前位置。

PHP版本

此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。

在线示例

以下示例演示了mysqli_field_tell()函数的用法(面向过程风格),取得所有字段的字段信息,然后通过 mysqli_field_tell() 取得当前字段并输出字段名称、表名和数据类型:

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("创建表.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   print("插入记录.....\n");

   //检索表的内容
   $res = mysqli_query($con, "SELECT * FROM myplayers");

   //获取字段
   while($info = mysqli_fetch_field($res)){
      //当前字段
      $currentfield = mysqli_field_tell($res);
      print("Current Field: ".$currentfield."\n");
      print("Name: ".$info->name."\n");
      print("Table: ".$info->table."\n");
      print("Type: ".$info->type."\n");
   }

   //结束语句
   mysqli_free_result($res);

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

输出结果

创建表.....
插入记录.....
Current Field: 1
Name: ID
Table: myplayers
Type: 3
Current Field: 2
Name: First_Name
Table: myplayers
Type: 253
Current Field: 3
Name: Last_Name
Table: myplayers
Type: 253
Current Field: 4
Name: Place_Of_Birth
Table: myplayers
Type: 253
Current Field: 5
Name: Country
Table: myplayers
Type: 253

在线示例

在面向对象风格中,此函数的语法为$result-> current_field;。以下是面向对象风格中此函数获取当前字段,并返回字段名的示例;

<?php
   //建立连接
   $con = new mysqli("localhost", "root", "password", "mydb");

   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("创建表.....\n");

   $stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';

   //执行语句
   $stmt->execute();

   //检索结果
   $result = $stmt->get_result();

   //Current Field
   $info = $result->fetch_field();

   $field = $result->current_field;
   print("Current Field: ".$field."\n");
   print("Field Name: ".$info->name);

   //结束语句
   $stmt->close();

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

输出结果

创建表.....
Current Field: 1
Field Name: Name

PHP MySQLi 参考手册

PHP mysqli_fetch_array() PHP mysqli_data_seek()