• PHP Video Tutorials

PHP mysqli_stmt_prepare() Function



Definition and Usage

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

Syntax

mysqli_stmt_prepare($stmt, $str);

Parameters

Sr.No Parameter & 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 Values

This function returns a boolean vcalue which is true incase of success and false incase of failure.

PHP Version

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

Example

Following example demonstrates the usage of the mysqli_stmt_prepare() function (in 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 = '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);
?>

This will produce following result −

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

Example

In object oriented style the syntax of this function is $stmt-> prepare(); Following is the example of this function in 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 = 'Raju';
   $Age = 25;
   print("Record Inserted.....");

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

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

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

This will produce following result −

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

Example

Let us see is another example of this function useing the SELECT query (in 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, 'Sikhar', 'Dhawan', 'Delhi', 'India')");
   $con -> query("INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   $con -> query("INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   $con -> query("INSERT INTO myplayers values(4, 'Virat', 'Kohli', 'Delhi', 'India')");
   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 = "India";

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

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

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

This will produce following result −

Table Created.....
Records Inserted.....
php_function_reference.htm
Advertisements