查詢

mysqli_result::$field_count()函式—用法及示例

「 返回結果集中的欄位數量 」


函式 mysqli_result::$field_count() 返回結果集中的欄位數量。

用法:

int mysqli_result::$field_count ( void )

示例:

// 連線到資料庫
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
    die("連線失敗: " . $mysqli->connect_error);
}

// 執行查詢語句
$result = $mysqli->query("SELECT * FROM table");

// 獲取結果集中的欄位數量
$fieldCount = $result->field_count;
echo "結果集中的欄位數量為: " . $fieldCount;

// 釋放結果集和關閉資料庫連線
$result->free();
$mysqli->close();

以上示例中,我們首先使用 mysqli 類連線到資料庫。然後,我們執行一個查詢語句並將結果儲存在 $result 變數中。接下來,我們使用 mysqli_result::$field_count() 函式獲取結果集中的欄位數量,並將其儲存在 $fieldCount 變數中。最後,我們輸出欄位數量。

請注意,在使用 mysqli_result::$field_count() 函式之前,必須先執行查詢語句並獲取結果集。同時,在完成使用結果集後,應該使用 mysqli_result::free() 方法釋放結果集,並使用 mysqli::close() 方法關閉資料庫連線。

補充糾錯
熱門PHP函式
分享連結