PHP mysqli_stmt_param_count() 函数用法及示例 - PHP教程

由网友 大卫 发布 阅读 2

PHP mysqli_stmt_param_count()  函数用法及示例 - PHP教程

PHP MySQLi 参考手册

mysqli_stmt_param_count()函数返回给定语句的参数的数量。

定义和用法

mysqli_stmt_param_count()函数接受一个(准备好的)语句对象作为参数,并返回其中的参数标记数。

语法

mysqli_stmt_param_count($stmt)

参数

序号参数及说明
1

stmt(必需)

这是表示执行SQL查询的语句的对象。

返回值

PHP mysqli_stmt_param_count()函数返回一个整数值,该整数值指示给定的预处理语句中参数标记的数量。

PHP版本

此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。

在线示例

假设我们已经在MySQL数据库中创建了一个名为employee的表,其内容如下:

mysql> select * from employee;
+------------+--------------+------+------+--------+
| FIRST_NAME | LAST_NAME    | AGE  | SEX  | INCOME |
+------------+--------------+------+------+--------+
| Vinay      | Bhattacharya |   20 | M    |  21000 |
| Sharukh    | Sheik        |   25 | M    |  23300 |
| Trupthi    | Mishra       |   24 | F    |  51000 |
| Sheldon    | Cooper       |   25 | M    |   2256 |
| Sarmista   | Sharma       |   28 | F    |  15000 |
+------------+--------------+------+------+--------+
5 rows in set (0.00 sec)

以下示例演示了 mysqli_stmt_param_count() 函数的用法(面向过程风格)-

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   $stmt = mysqli_prepare($con, "UPDATE employee set INCOME=INCOME-? where INCOME>=?");
   mysqli_stmt_bind_param($stmt, "si", $reduct, $limit);
   $limit = 20000;
   $reduct = 5000;

   //执行语句
   mysqli_stmt_execute($stmt);
   print("记录已更新......\n");

   //受影响的行
   $count = mysqli_stmt_param_count($stmt);

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

   //关闭连接
   mysqli_close($con);

   print("受影响的行 ".$count);
?>

输出结果

记录已更新......
受影响的行 3

在线示例

在面向对象风格中,此函数的语法为$stmt->param_count;。以下是面向对象风格中此函数的示例;

<?php
   //建立连接
   $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("创建表.....\n");

   $stmt = $con -> prepare( "INSERT INTO myplayers values(?, ?, ?, ?, ?)");
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';

   //执行语句
   $stmt->execute();

   //记录已更新
   $count = $stmt ->param_count;
   print("参数数量: ".$count);

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

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

输出结果

参数数量: 5

PHP MySQLi 参考手册

PHP mysqli_stmt_attr_set() PHP mysqli_num_rows()