mysqli_field_tell()函數(shù)返回字段指針的位置。
一個PHP結(jié)果對象(類mysqli_result)表示由SELECT或DESCRIBE或EXPLAIN查詢返回的MySQL結(jié)果。結(jié)果對象的字段光標(biāo)/指針指向其中的字段(列值)。
mysqli_field_tell()函數(shù)接受一個結(jié)果對象作為參數(shù),檢索和返回在給定對象中的字段指針的當(dāng)前位置。
mysqli_field_tell($result);
| 序號 | 參數(shù)及說明 |
|---|---|
| 1 | result(必需) 這是表示結(jié)果對象的標(biāo)識符。 |
PHP mysqli_field_tell()函數(shù)返回一個整數(shù)值,該值指定字段指針在給定結(jié)果對象中的當(dāng)前位置。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_field_tell()函數(shù)的用法(面向過程風(fēng)格),取得所有字段的字段信息,然后通過 mysqli_field_tell() 取得當(dāng)前字段并輸出字段名稱、表名和數(shù)據(jù)類型:
<?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("創(chuàng)建表.....\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");
//檢索表的內(nèi)容
$res = mysqli_query($con, "SELECT * FROM myplayers");
//獲取字段
while($info = mysqli_fetch_field($res)){
//當(dāng)前字段
$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");
}
//結(jié)束語句
mysqli_free_result($res);
//關(guān)閉連接
mysqli_close($con);
?>輸出結(jié)果
創(chuàng)建表..... 插入記錄..... 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
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$result-> current_field;。以下是面向?qū)ο箫L(fēng)格中此函數(shù)獲取當(dāng)前字段,并返回字段名的示例;
<?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("創(chuàng)建表.....\n");
$stmt = $con -> prepare( "SELECT * FROM Test WHERE Name in(?, ?)");
$stmt -> bind_param("ss", $name1, $name2);
$name1 = 'Raju';
$name2 = 'Rahman';
//執(zhí)行語句
$stmt->execute();
//檢索結(jié)果
$result = $stmt->get_result();
//Current Field
$info = $result->fetch_field();
$field = $result->current_field;
print("Current Field: ".$field."\n");
print("Field Name: ".$info->name);
//結(jié)束語句
$stmt->close();
//關(guān)閉連接
$con->close();
?>輸出結(jié)果
創(chuàng)建表..... Current Field: 1 Field Name: Name