目录

PHP mysqli prepare() 函数

❮ 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 = "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+

❮ PHP MySQLi 参考