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

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP mysqli_stmt_init()

PHP MySQLi Reference Manual

The mysqli_stmt_init() function initializes a statement and returns an object used by mysqli_stmt_prepare().

Definition and Usage

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

Syntax

mysqli_stmt_init($con);

Parameter

Serial NumberParameters and Description
1

con(Required)

This is an object representing the connection with the MySQL Server.

Return Value

This function returns a statement object.

PHP Version

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

Online Example

The following example demonstratesmysqli_stmt_init()Function Usage (Procedural Style)-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   mysqli_query($con, $query);
   //Initialize 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("Insert record.....");
   //Execute statement
   mysqli_stmt_execute($stmt);
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Insert record.....

Online Example

Here is another example of this function, initializing the declaration and returning the object used by mysqli_stmt_prepare():

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   $query = "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"; 
   $con->query($query);
   //Initialize statement
   $stmt =  $con->stmt_init();
   $res = $stmt->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

Insert record.....

PHP MySQLi Reference Manual