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
廣告