• PHP Video Tutorials

PHP mysqli_stmt_init() Function



Definition and Usage

The mysqli_stmt_init() function is used to initialize a statement object. The result of this function can be passed as one of the parameter to the mysqli_stmt_prepare() function.

Syntax

mysqli_stmt_init($con);

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

Return Values

This function returns a statement object.

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_init() function (in procedural style) −

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

   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   mysqli_query($con, $query);

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

   $res = 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 −

Record Inserted.....

Example

Following is another example of this function $minus;

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

   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   $con->query($query);

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

   $res = $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 −

Record Inserted.....
php_function_reference.htm
Advertisements