PHP mysqli prepare()関数
例-オブジェクト指向スタイル
実行する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歳以上 |
❮PHPMySQLiリファレンス