2024 年日本 PHP 研討會

mysqli_stmt::$field_count

mysqli_stmt_field_count

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$field_count -- mysqli_stmt_field_count傳回指定陳述式中的欄位數

說明

物件導向風格

程序式風格

mysqli_stmt_field_count(mysqli_stmt $statement): int

傳回預備語句中的欄位數量。

參數

statement

僅限程序式風格:由 mysqli_stmt_init() 傳回的 mysqli_stmt 物件。

返回值

傳回一個整數,表示欄位的數量。

範例

範例 #1 物件導向風格

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$code = 'FR';

$stmt = $mysqli->prepare("SELECT Name FROM Country WHERE Code=?");
$stmt->bind_param('s', $code);
$stmt->execute();
$row = $stmt->get_result()->fetch_row();
for (
$i = 0; $i < $stmt->field_count; $i++) {
printf("第 %d 個欄位的值為 %s", $i, $row[$i]);
}

範例 #2 程序式風格

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");

$code = 'FR';

$stmt = mysqli_prepare($mysqli, "SELECT Name FROM Country WHERE Code=?");
mysqli_stmt_bind_param($stmt, 's', $code);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_row($result);
for (
$i = 0; $i < mysqli_stmt_field_count($stmt); $i++) {
printf("Value of column number %d is %s", $i, $row[$i]);
}

以上範例將輸出類似以下的內容:

Value of column number 0 is France

另請參閱

新增註解

使用者貢獻的註解

此頁面沒有使用者貢獻的註解。
To Top