English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The mysqli_prepare() function prepares to execute an SQL statement
The mysqli_prepare() function prepares the execution of an SQL statement, returns a statement handle, and can perform subsequent operations on this handle. You can use parameter markers ("?") in this query to specify values for it, and then execute it later.
Here it only supports a single SQL statement, and does not support multiple SQL statements.
Before executing the statement, you need to use the mysqli_stmt_bind_param() function to bind placeholder parameters. Similarly, before retrieving the results, you must use the mysqli_stmt_bind_result() function to bind the returned column values.
mysqli_prepare($con, $str);
Serial number | Parameters and descriptions |
---|---|
1 | con(Required) This is an object representing the connection to the MySQL Server. |
2 | str(Required) This is the specified string value for the required query. |
If successful, this function returns a statement object, if failed, then returnsfalse.
This function was initially introduced in PHP version5introduced and can be used in all higher versions.
The following example demonstratesmysqli_prepare()Function usage (procedural style)-
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; mysqli_query($con, $query); print("Create table.....\n"); $stmt = mysqli_prepare($con, "INSERT INTO Test values(?, ?)"); mysqli_stmt_bind_param($stmt, "si", $Name, $Age); $Name = 'Raju'; $Age = 25; print("Insert record....."); //Execute statement mysqli_stmt_execute($stmt); //End statement mysqli_stmt_close($stmt); //Close connection mysqli_close($con); ?>
Output result
Create table..... Insert record.....
If you verify the content of the table as follows, $ minus;
mysql> select * from test; +------+------+ | Name | AGE | +------+------+ | Raju | 25 | +------+------+ 1 row in set (0.00 sec)
In the object-oriented style, the syntax of this function is$ con-> prepare();。Here is an example of the This function of the面向对象样式$ minus;
<?php //Establish connection $con = new mysqli("localhost", "root", "password", "mydb"); $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; $con -> query($query); print("Create table.....\n"); $stmt = $con -> prepare( "INSERT INTO Test values(?, ?)"); $stmt -> bind_param("si", $Name, $Age); $Name = 'Raju'; $Age = 25; print("Insert record....."); //Execute statement $stmt->execute(); //End statement $stmt->close(); //Close connection $con->close(); ?>
Output result
Create table..... Insert record.....