PHP | mysqli_stmt_prepare() Function

PHP mysqli_stmt_prepare() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial, we are going to be discussing about the built-in PHP mysqli_stmt_prepare() Function.

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

Syntax

Following below is the syntax to use this function -

mysqli_stmt_prepare($stmt, $str);


Parameter Details

Sr.NoParameter & Description
1

stmt(Mandatory)

This is an object representing a statement (returned by the mysqli_stmt_init() function).

2

str(Mandatory)

This is string value specifying the required query.


Return Value

This built-in PHP function returns TRUE on success and FALSE on failure.

PHP Version

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

Example1

The following below is an example which demonstrates the usage of the built-in PHP mysqli_stmt_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");

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

   mysqli_stmt_prepare($stmt, "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.....
Records Inserted.....

Example2

In an object oriented style the syntax of this function is $stmt->prepare(); Following is the example of this function in an object oriented style $minus;

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

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

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

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

Example3

Following below is another example of this built-in function using the SELECT query (in an object oriented style) -

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

   $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");
   $con -> query("INSERT INTO myplayers values(1, 'Kennedy', 'Nkpara', 'PortHarcourt', 'Nigeria')");
   $con -> query("INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   $con -> query("INSERT INTO myplayers values(3, 'Queen', 'Douglas', 'Texas', 'UnitedStates')");
   $con -> query("INSERT INTO myplayers values(4, 'Paul', 'Francis', 'Texas', 'UnitedStates')");
   print("Records Inserted.....\n");

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

   $stmt -> prepare("SELECT * FROM myplayers WHERE country=?");
   $stmt -> bind_param("s", $country);
   $country = "Nigeria";

   //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 below -

Table Created.....
Records 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 mysqli_stmt_reset() Function in PHP.

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.