mysqli_stmt_result_metadata()函數(shù)從準(zhǔn)備好的語句返回結(jié)果集元數(shù)據(jù)。
mysqli_stmt_result_metadata()函數(shù)接受預(yù)準(zhǔn)備語句對象作為參數(shù),如果給定語句執(zhí)行SELECT查詢(或任何其他返回結(jié)果集的查詢),則它(此函數(shù))返回一個元數(shù)據(jù)對象,該對象保存有關(guān)給定語句結(jié)果集的信息。
mysqli_stmt_result_metadata($stmt);
序號 | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是表示準(zhǔn)備好的語句的對象。 |
PHP mysqli_stmt_result_metadata()函數(shù)在成功時返回元數(shù)據(jù)對象,在失敗時返回false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_stmt_result_metadata()函數(shù)的用法(面向過程風(fēng)格)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE test(Name VARCHAR(255), age INT)"); mysqli_query($con, "INSERT INTO test values('Raju', 25)"); mysqli_query($con, "INSERT INTO test values('Jonathan', 30)"); print("創(chuàng)建表.....\n"); //檢索表的內(nèi)容 $stmt = mysqli_prepare($con, "SELECT * FROM test"); //執(zhí)行語句 mysqli_stmt_execute($stmt); //檢索結(jié)果設(shè)置元數(shù)據(jù) $metadata = mysqli_stmt_result_metadata($stmt); print_r(mysqli_fetch_fields($metadata)); mysqli_free_result($metadata); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... Array ( [0] => stdClass Object ( [name] => Name [orgname] => Name [table] => test [orgtable] => test [def] => [db] => mydb [catalog] => def [max_length] => 0 [length] => 765 [charsetnr] => 33 [flags] => 0 [type] => 253 [decimals] => 0 ) [1] => stdClass Object ( [name] => AGE [orgname] => AGE [table] => test [orgtable] => test [def] => [db] => mydb [catalog] => def [max_length] => 0 [length] => 11 [charsetnr] => 63 [flags] => 32768 [type] => 3 [decimals] => 0 ) )
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$stmt-> result_metadata();。以下是面向?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'; print("插入記錄.....\n"); //執(zhí)行語句 $stmt->execute(); //檢索結(jié)果設(shè)置元數(shù)據(jù) $metadata = $stmt->result_metadata(); $field = $metadata->fetch_field(); print("字段名稱: ".$field->name); //結(jié)束語句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... 字段名稱: Name