當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP mysqli_stmt_result_metadata()用法及代碼示例



定義和用法

這個mysqli_stmt_result_metadata()函數接受準備好的語句對象作為參數,如果給定的語句執行 SELECT 查詢(或任何其他返回結果集的查詢),它(此函數)返回一個元數據對象,該對象保存有關給定語句的結果集的信息。

用法

mysqli_stmt_result_metadata($stmt);

參數

Sr.No 參數及說明
1

con(Mandatory)

這是一個表示準備好的語句的對象。

返回值

PHP mysqli_stmt_result_metadata() 函數在成功時返回元數據對象,失敗時返回 false。

PHP版本

這個函數最初是在 PHP 版本 5 中引入的,適用於所有後續版本。

示例

以下示例演示了 mysqli_stmt_result_metadata() 函數的用法(程序風格) -

<?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("Table Created.....\n");

   //Retrieving the contents of the table
   $stmt = mysqli_prepare($con, "SELECT * FROM test");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Retrieving the resultset metadata
   $metadata = mysqli_stmt_result_metadata($stmt);
   print_r(mysqli_fetch_fields($metadata));
 
   mysqli_free_result($metadata);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果 -

Table Created.....
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
        )

)

示例

在麵向對象風格中,這個函數的語法是 $stmt->result_metadata();以下是麵向對象樣式 $minus 中此函數的示例;

<?php
   //Creating a connection
   $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("Table Created.....\n");

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

   //Executing the statement
   $stmt->execute();

   //Retrieving the resultset metadata
   $metadata = $stmt->result_metadata();

   $field = $metadata->fetch_field();
   print("Field Name:".$field->name);

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

這將產生以下結果 -

Table Created.....
Records Inserted.....
Field Name:Name

相關用法


注:本文由純淨天空篩選整理自 PHP mysqli_stmt_result_metadata() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。