PHP mysqli stmt_init() Method
The PHP mysqli::stmt_init() / mysqli_stmt_init() function allocates and initializes a statement object for use with mysqli_stmt_prepare().
Note: Any subsequent calls to any mysqli_stmt function will fail until mysqli_stmt_prepare() was called.
Syntax
//Object-oriented style public mysqli::stmt_init() //Procedural style mysqli_stmt_init(mysql)
Parameters
mysql |
Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init(). |
Return Value
Returns an object.
Example: Object-oriented style
The example below shows the usage of mysqli::stmt_init() method.
<?php //establishing connection to the database $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: ". $mysqli->connect_error; exit(); } //creating a prepared statement $stmt = $mysqli->stmt_init(); $query = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)"; $stmt->prepare($query); //binding parameters $stmt->bind_param('ssd', $name, $city, $salary); //set parameters and execute $name = "John"; $city = "London"; $salary = 2800; $stmt->execute(); $name = "Marry"; $city = "Paris"; $salary = 2850; $stmt->execute(); echo "Records inserted successfully."; //closing the connection $mysqli->close(); ?>
The output of the above code will be similar to:
Records inserted successfully.
Example: Procedural style
The example below shows the usage of mysqli_stmt_init() function.
<?php //establishing connection to the database $mysqli = mysqli_connect("localhost", "user", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); exit(); } //creating a prepared statement $stmt = mysqli_stmt_init($mysqli); $query = "INSERT INTO Employee (Name, City, Salary) VALUES (?, ?, ?)"; mysqli_stmt_prepare($stmt, $query); //binding parameters mysqli_stmt_bind_param($stmt, 'ssd', $name, $city, $salary); //set parameters and execute $name = "John"; $city = "London"; $salary = 2800; mysqli_stmt_execute($stmt); $name = "Marry"; $city = "Paris"; $salary = 2850; mysqli_stmt_execute($stmt); echo "Records inserted successfully."; //closing the connection mysqli_close($mysqli); ?>
The output of the above code will be similar to:
Records inserted successfully.
❮ PHP MySQLi Reference