PHP -mysqli-prepare()- Funktion
Beispiel – Objektorientierter Stil
Bereiten Sie eine SQL-Anweisung zur Ausführung vor:
<?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 = "[email protected]";
$stmt -> execute();
$firstname = "Mary";
$lastname = "Moe";
$email = "[email protected]";
$stmt -> execute();
echo "New records created successfully";
$stmt -> close();
$mysqli -> close();
?>
Definition und Verwendung
Die Funktion Prepare() / mysqli_prepare() wird verwendet, um eine SQL-Anweisung für die Ausführung vorzubereiten.
Syntax
Objektorientierter Stil:
$mysqli -> prepare(query)
Verfahrensstil:
mysqli_prepare(connection, query)
Parameterwerte
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! |
Technische Details
Rückgabewert: | Ein Aussageobjekt zum Erfolg. FALSE bei Fehler |
---|---|
PHP-Version: | 5+ |
❮ PHP MySQLi-Referenz