mysqli_fetch_fields()函數(shù)返回結(jié)果集中代表字段(列)的對象的數(shù)組。
PHP結(jié)果對象(屬于mysqli_result類)表示由SELECT或DESCRIBE或EXPLAIN查詢返回的MySQL結(jié)果。
mysqli_fetch_fields函數(shù)的作用是:接受一個結(jié)果對象作為參數(shù),并返回一個對象數(shù)組,每個對象表示結(jié)果中的一個字段。
mysqli_fetch_fields($result);
序號 | 參數(shù)及說明 |
---|---|
1 | result(必需) 這是表示結(jié)果對象的標(biāo)識符。 |
PHP mysqli_fetch_fields()函數(shù)返回一個對象數(shù)組,其中每個對象都包含給定結(jié)果中字段的定義信息。如果沒有信息,此函數(shù)將返回FALSE。
返回數(shù)組的對象包含以下屬性$ minus;。
name - 列名
orgname - 原始的列名(如果指定了別名)
table - 表名
orgtable - 原始的表名(如果指定了別名)
def - 該字段的默認(rèn)值
max_length - 字段的最大寬度
length - 在表定義中指定的字段寬度
charsetnr - 字段的字符集號
flags - 字段的位標(biāo)志
type - 用于字段的數(shù)據(jù)類型
decimals - 整數(shù)字段,小數(shù)點后的位數(shù)
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_fetch_fields()函數(shù)的用法(面向過程風(fēng)格)-
<?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"); //獲取所有字段 $info = mysqli_fetch_fields($res); foreach ($info as $ele) { print("ID: ".$ele->name."\n"); print("First_Name: ".$ele->table."\n"); print("Last_Name: ".$ele->max_length."\n"); print("Place_Of_Birth: ".$ele->charsetnr."\n"); print("Country: ".$ele->flags."\n"); print("Country: ".$ele->type."\n"); print("\n"); } //結(jié)束語句 mysqli_free_result($res); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... ID: ID First_Name: myplayers Last_Name: 1 Place_Of_Birth: 63 Country: 32768 Country: 3 ID: First_Name First_Name: myplayers Last_Name: 8 Place_Of_Birth: 33 Country: 0 Country: 253 ID: Last_Name First_Name: myplayers Last_Name: 10 Place_Of_Birth: 33 Country: 0 Country: 253 ID: Place_Of_Birth First_Name: myplayers Last_Name: 8 Place_Of_Birth: 33 Country: 0 Country: 253 ID: Country First_Name: myplayers Last_Name: 11 Place_Of_Birth: 33 Country: 0 Country: 253
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$result-> fetch_fields();。以下是面向?qū)ο箫L(fēng)格此函數(shù)的示例;
<?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(); //獲取所有行作為數(shù)組 $info = $result->fetch_fields(); foreach ($info as $ele) { print("ID: ".$ele->name."\n"); print("First_Name: ".$ele->table."\n"); print("Last_Name: ".$ele->max_length."\n"); print("Place_Of_Birth: ".$ele->charsetnr."\n"); print("Country: ".$ele->flags."\n"); print("Country: ".$ele->type."\n"); print("\n"); } //結(jié)束語句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>