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


PHP mysqli_stmt_field_count()用法及代碼示例


定義和用法

這個mysqli_stmt_field_count()函數接受一個語句對象作為參數,並返回給定語句結果中的字段數。

用法

mysqli_stmt_field_count($stmt)

參數

Sr.No 參數及說明
1

stmt(Mandatory)

這是一個對象,表示執行 SQL 查詢的語句。

返回值

PHP mysqli_stmt_field_count() 函數返回一個整數值,指示語句返回的結果集中的行數。

PHP版本

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

示例

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

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

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

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Field Count 
   $count = mysqli_stmt_field_count($stmt);
   print("Field Count:".$count);

   //Closing the statement
   mysqli_stmt_close($stmt);

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

這將產生以下結果 -

Table Created.....
Record Inserted.....
Field Count:5

示例

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

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created.....\n");
   $con -> query("INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   $con -> query("INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   print("Records Inserted.....\n");

   //Retrieving Data
   $stmt = $con ->prepare("SELECT First_Name, Last_Name, Country FROM myplayers");

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

   //Field Count
   $count = $stmt->field_count;
   print("Field Count:".$count);

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

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

這將產生以下結果 -

Table Created.....
Records Inserted.....
Field Count:3

相關用法


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