English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP mysqli_prepare() function

PHP MySQLi Reference Manual

The mysqli_prepare() function prepares to execute an SQL statement

Definition and usage

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.

Syntax

mysqli_prepare($con,  $str);

Parameter

Serial numberParameters 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.

Return value

If successful, this function returns a statement object, if failed, then returnsfalse.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Online example

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)

Online example

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.....

PHP MySQLi Reference Manual