PHP mysqli_stmt_init() 函式



定義和用法

mysqli_stmt_init() 函式用於初始化一個語句物件。此函式的結果可以作為 mysqli_stmt_prepare() 函式的引數之一。

語法

mysqli_stmt_init($con);

引數

序號 引數及說明
1

con(必填)

這是一個表示與 MySQL 伺服器連線的物件。

返回值

此函式返回一個語句物件。

PHP 版本

此函式首次出現在 PHP 5 版本中,並在所有後續版本中均可使用。

示例

以下示例演示了 mysqli_stmt_init() 函式的用法(過程式風格)−

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

   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   mysqli_query($con, $query);

   //initiating the statement
   $stmt =  mysqli_stmt_init($con);

   $res = mysqli_stmt_prepare($stmt, "INSERT INTO Test values(?, ?)");
   mysqli_stmt_bind_param($stmt, "si", $Name, $Age);
   $Name = 'Raju';
   $Age = 25;
   print("Record Inserted.....");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Closing the statement
   mysqli_stmt_close($stmt);

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

這將產生以下結果 −

Record Inserted.....

示例

以下是此函式的另一個示例 $minus;

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

   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   $con->query($query);

   //initiating the statement
   $stmt =  $con->stmt_init();

   $res = $stmt->prepare("INSERT INTO Test values(?, ?)");
   $stmt->bind_param("si", $Name, $Age);
   $Name = 'Raju';
   $Age = 25;
   print("Record Inserted.....");

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

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

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

這將產生以下結果 −

Record Inserted.....
php_function_reference.htm
廣告