准备一条要执行的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 = "john@example.com";
$stmt -> execute();
$firstname = "Mary";
$lastname = "Moe";
$email = "mary@example.com";
$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+ |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!