PHP | mysqli_prepare() Function

PHP mysqli_prepare() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_prepare() Function.

The built-in PHP mysqli_prepare() function prepares an SQL statement for execution, you can make use of parameter markers ("?") in this query, specify values for them, and them do the execution later.

Syntax

Following below is the syntax to use this function -

mysqli_prepare($con,  $str);


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

str(Mandatory)

This is string value specifying the required query.


Return Value

This function returns a statement object on success and false on failure.

PHP Version

This PHP function was first introduced in PHP version 5 and it works in all the later versions.

Example1

The following example illustrates the usage of the PHP mysqli_prepare() function (in a procedural style) -

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

   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   mysqli_query($con, $query);
   print("Table Created.....\n");

   $stmt = mysqli_prepare($con, "INSERT INTO Test values(?, ?)");
   mysqli_stmt_bind_param($stmt, "si", $Name, $Age);
   $Name = 'Kennedy';
   $Age = 27;
   print("Record Inserted.....");

   //Executing the statement
   mysqli_stmt_execute($stmt);

   //Closing the statement
   mysqli_stmt_close($stmt);

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

Output

When the above code is executed, it will produce the following result -

Table Created.....
Record Inserted.....

If you observe the contents of the table in the database you can see all the inserted records as shown below -

mysql> select * from test;
+---------+---------+
| Name    | AGE     |
+---------+---------+
| Kennedy | 27      |
+---------+---------+
1 row in set (0.00 sec)

Example2

In an object oriented style the syntax of this built-in function is $con->prepare(); The following below is an example of this PHP function in an object oriented style $minus;

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

   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   $con -> query($query);
   print("Table Created.....\n");

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

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

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

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

Output

When the above code is executed, it will produce the following result -

Table Created.....
Record Inserted.....


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP mysqli_query() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.