PHP mysqli เตรียม ()ฟังก์ชั่น
ตัวอย่าง - สไตล์เชิงวัตถุ
เตรียมคำสั่ง SQL เพื่อดำเนินการ:
<?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();
?>
ความหมายและการใช้งาน
ฟังก์ชัน prepare() / mysqli_prepare() ใช้เพื่อเตรียมคำสั่ง SQL สำหรับดำเนินการ
ไวยากรณ์
สไตล์เชิงวัตถุ:
$mysqli -> prepare(query)
รูปแบบขั้นตอน:
mysqli_prepare(connection, query)
ค่าพารามิเตอร์
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! |
รายละเอียดทางเทคนิค
คืนมูลค่า: | วัตถุคำสั่งเกี่ยวกับความสำเร็จ FALSE เมื่อล้มเหลว |
---|---|
รุ่น PHP: | 5+ |
❮ PHP MySQLi อ้างอิง