LoginSignup
0

More than 3 years have passed since last update.

mysql プリペアドステートメント

Posted at

プリペアドステートメントとは??

sqlのコードをわかりやすくしたテンプレートのようなものです。

書き方
// SQL文の作成
$stmt = $mysqli->prepare("INSERT INTO nekosan (
name, price, better_before, modify_datetime, create_datetime
) VALUES (
?, ?, ?, ?, ?
)");
// パラメータを設定
$stmt->bind_param( 'sisss', $name, $price, $better_before, $date,
$date);

// 実行
$res = $stmt->execute();
$stmt->close();

name, price, better_before.png

‘sis’とは、$nameや$priceのデータ型を意味しています。

$nameはstring 「s」

$priceはinteger 「i」

$better_beforeはstring 「s」

?(ハテナ)には、bind_paramで指定したデータが入ってきます。

参考記事; https://gray-code.com/php/prepared-statement-by-mysqli/

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0