Hàm chuẩn bị () mysqli trong PHP
Ví dụ - Kiểu hướng đối tượng
Chuẩn bị một câu lệnh SQL để thực thi:
<?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();
?>
Định nghĩa và Cách sử dụng
Hàm chuẩn bị () / mysqli_prepare () được sử dụng để chuẩn bị thực thi một câu lệnh SQL.
Cú pháp
Phong cách hướng đối tượng:
$mysqli -> prepare(query)
Phong cách thủ tục:
mysqli_prepare(connection, query)
Giá trị tham số
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! |
Chi tiết kỹ thuật
Giá trị trả lại: | Một đối tượng tuyên bố về thành công. FALSE khi thất bại |
---|---|
Phiên bản PHP: | 5+ |
❮ Tham khảo PHP MySQLi