PHP mysqli prepare() Function
Example - Object Oriented style
Prepare an SQL statement for execution:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Check connection
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// prepare and bind
$stmt = $mysqli -> prepare("INSERT INTO
MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt ->
bind_param("sss", $firstname, $lastname, $email);
// set parameters and
execute
$firstname
= "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt -> execute();
$firstname = "Mary";
$lastname = "Moe";
$email = "mary@example.com";
$stmt -> execute();
echo "New records created successfully";
$stmt -> close();
$mysqli -> close();
?>
Definition and Usage
The prepare() / mysqli_prepare() function is used to prepare an SQL statement for execution.
Syntax
Object oriented style:
$mysqli -> prepare(query)
Procedural style:
mysqli_prepare(connection, query)
Parameter Values
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies an SQL query. Note: Do not add semicolon to the end of the query! |
Technical Details
Return Value: | A statement object on success. FALSE on failure |
---|---|
PHP Version: | 5+ |
❮ PHP MySQLi Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.