PHP Conference Japan 2024

mysqli_stmt::$param_count

mysqli_stmt_param_count

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$param_count -- mysqli_stmt_param_count返回给定语句的参数数量

描述

面向对象风格

过程化风格

mysqli_stmt_param_count(mysqli_stmt $statement): int

返回准备好的语句中存在的参数标记数量。

参数

statement

仅过程化风格:由 mysqli_stmt_init() 返回的 mysqli_stmt 对象。

返回值

返回一个整数,表示参数的数量。

示例

示例 #1 面向对象风格

<?php
$mysqli
= new mysqli("localhost", "my_user", "my_password", "world");

/* 检查连接 */
if (mysqli_connect_errno()) {
printf("连接失败:%s\n", mysqli_connect_error());
exit();
}

if (
$stmt = $mysqli->prepare("SELECT Name FROM Country WHERE Name=? OR Code=?")) {

$marker = $stmt->param_count;
printf("语句有 %d 个标记。\n", $marker);

/* 关闭语句 */
$stmt->close();
}

/* 关闭连接 */
$mysqli->close();
?>

示例 #2 过程化风格

<?php
$link
= mysqli_connect("localhost", "my_user", "my_password", "world");

/* 检查连接 */
if (mysqli_connect_errno()) {
printf("连接失败:%s\n", mysqli_connect_error());
exit();
}

if (
$stmt = mysqli_prepare($link, "SELECT Name FROM Country WHERE Name=? OR Code=?")) {

$marker = mysqli_stmt_param_count($stmt);
printf("语句有 %d 个标记。\n", $marker);

/* 关闭语句 */
mysqli_stmt_close($stmt);
}

/* 关闭连接 */
mysqli_close($link);
?>

以上示例将输出

Statement has 2 markers.

参见

添加注释

用户贡献的注释 1 条注释

2
Senthryl
15 年前
如果语句没有正确准备或根本没有准备,则此参数(大概还有 mysqli_stmt 中的任何其他参数)将引发一条错误消息“属性访问尚不允许”。

为防止这种情况,请始终确保“prepare”语句的返回值为真,然后再访问这些属性。
To Top