LoginSignup
3
2

More than 5 years have passed since last update.

PHPからMySQLに接続してレコードの更新・削除を行う

Posted at

前提

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

テーブル

テーブル: hogetable に以下のようなレコードが存在する

id name pass
1 taro foofoo
2 taichi barbar
3 kosuke foofoo

コード(処理部分のみ)

name が t で始まる場合の pass を hogehoge に変更する

$stmt = $dbh->prepare("update hogetable set pass = :pass where name like :name");
$stmt->execute(array(":pass"=>"hogehoge", ":name"=>"t%"));

echo $stmt->rowCount() . "records updated";    // rowCountで処理が走った行数を取得して表示させる

結果

id name pass
1 taro hogehoge
2 taichi hogehoge
3 kosuke foofoo

出力
2records updated

pass が foofoo であるものを削除する

$stmt = $dbh->prepare("delete from hogetable where pass = :pass");
$stmt->execute(array(":pass"=>"foofoo"));

echo $stmt->rowCount() . " records deleted";

結果

id name pass
2 taichi barbar

出力
2records deleted

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