PHP MySQLi 函数

PHP mysqli_stmt::$param_count / mysqli_stmt_param_count() 函数返回准备好的语句中存在的参数标记的数量.

语法

//面向对象风格
$mysqli_stmt->param_count;

//面向过程风格
mysqli_stmt_param_count(statement)

参数

statement 必需。 仅适用于面向过程风格:指定 mysqli_stmt_init() 返回的 mysqli_stmt 对象。

返回值

返回整数表示参数的数量。

示例:面向对象风格

下面的示例显示了mysqli_stmt::$param_count属性的用法。

<?php
//建立与数据库的连接
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_param_count) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

$sql = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)";
if ($stmt = $mysqli->prepare($sql)) {

  $marker = $stmt->param_count;
  printf("Statement has %d markers.\n", $marker);

  //结束语句
  $stmt->close();
}

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

上述代码的输出将类似于:

Statement has 3 markers.

示例:面向过程风格

下面的示例显示mysqli_stmt_param_count 的用法() 函数。

<?php
//建立与数据库的连接
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_param_count()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

$sql = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)";
if ($stmt = mysqli_prepare($mysqli, $sql)) {

  $marker = mysqli_stmt_param_count($stmt);
  printf("Statement has %d markers.\n", $marker);

  //结束语句
  mysqli_stmt_close($stmt);
}

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

上述代码的输出将类似于:

Statement has 3 markers.