PHP mysqli_stmt_bind_param() 函数



定义和用法

mysqli_stmt_bind_param() 函数用于将变量绑定到预处理语句的参数标记。

语法

mysqli_stmt_bind_param($stmt, $types, $var1, $var2...);

参数

序号 参数及描述
1

stmt(必填)

这是一个表示预处理语句的对象。

2

types(必填)

一个字符串(由单个字符组成),指定变量的类型,其中:

  • i 代表整数类型

  • d 代表双精度浮点数类型

  • s 代表字符串类型

  • b 代表二进制大对象类型

3

var(必填)

变量的值,用逗号分隔。

返回值

PHP mysqli_stmt_bind_param() 函数返回一个布尔值,成功时返回 true,失败时返回 false

PHP 版本

此函数首次引入于 PHP 5 版本,并在所有后续版本中可用。

示例

以下示例演示了 mysqli_stmt_bind_param() 函数的用法(过程式风格):

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Creating a table
   $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created.....\n");

   //Inserting values into the table using prepared statement
   $stmt = $con -> prepare( "INSERT INTO myplayers values(?, ?, ?, ?, ?)");

   //Binding values to the parameter markers
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';

   //Executing the statement
   $stmt->execute();
   //Closing the statement
   $stmt->close();
   //Closing the connection
   $con->close();
?>

这将产生以下结果:

Table Created.....

示例

在面向对象风格中,此函数的语法是 $stmt->close(); 以下是此函数在面向对象风格中的示例:

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Creating a table
   $con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   print("Table Created.....\n");

   //Inserting values into the table using prepared statement
   $stmt = $con -> prepare( "INSERT INTO myplayers values(?, ?, ?, ?, ?)");

   //Binding values to the parameter markers
   $stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
   $id = 1;
   $fname = 'Shikhar';
   $lname = 'Dhawan';
   $pob = 'Delhi';
   $country = 'India';

   //Executing the statement
   $stmt->execute();

   //Closing the statement
   $stmt->close();

   //Closing the connection
   $con->close();
?>

这将产生以下结果:

Table Created.....

示例

以下是此函数的另一个示例:

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

   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Table Created.....\n");
   mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
   print("Records Inserted.....\n");

   $stmt = mysqli_prepare($con, "DELETE FROM test where Age<?");
   mysqli_stmt_bind_param($stmt, "i", $num);
   $num = 28;
   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Closing the statement
   mysqli_stmt_close($stmt);
   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果:

Table Created.....
php_function_reference.htm
广告