LoginSignup
3
3

More than 5 years have passed since last update.

PHPからMySQLに接続してレコードを挿入する2

Posted at

前提

PHPからMySQLに接続する にて接続を行っている

コード(処理部分のみ)

テーブル: hogetable に以下のようなレコードを挿入する。

id name pass
2 taro foo
$stmt = $dbh->prepare("insert into hogetable (id,name,pass) values (:id,:name,:pass)");

$stmt->bindParam(":id", $id);
$stmt->bindParam(":name", $name);
$stmt->bindParam(":pass", $pass);

$id = "2";
$name = "taro";
$pass = "foo";

$stmt->execute();    // ここで変数に入れた値をもちいてinsertする。

echo $dbh->lastInsertId();    // 最後に挿入したレコードのidまたはシーケンス番号を表示させる。

その他

bindParamでプレースホルダーに変数をもたせる。

3
3
0

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
3
3