DB接続
$dsn = 'mysql:dbname={DBの名前};host=localhost;charset=utf8';
$user = '{ユーザー名(xamppならroot)}';
$password = '{パスワード(xamppなら空白)}';
$dbh = new PDO($dsn,$user,$password);
$dbh -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//try-catch構文にするための文章
INSERT(テーブルに行を追加)
//この3つは適当
$string_1 = '文章1';
$string_2 = '文章2';
$string_3 = '文章3';
$sql = 'INSERT INTO {テーブル名} (column_1,column_2,culumn_3) VALUES (?,?,?)';
//prepare-executeで準備-実行
$stmt = $dbh -> prepare($sql);
$data[] = $string_1;
$data[] = $string_2;
$data[] = $string_3;
$stmt -> execute($data);
$dbh = null; //DB切断
SELECT(行を参照)
$int_0 = '{なんかの数字}';
$sql = 'SELECT column_1,column_2,culumn_3 FROM {テーブル名} WHERE column_0=?';
$stmt = $dbh -> prepare($sql);
$data[] = $int_1;
$stmt -> execute($data);
$rec = $stmt -> fetch(PDO::FETCH_ASSOC);
$string_1 = $rec['column_1'];
$string_2 = $rec['column_2'];
$string_3 = $rec['column_3'];
$dbh = null;
UPDATE(行を更新)
$int_0 = '{なんかの数字}';
$string_1 = '文章1';
$string_2 = '文章2';
$sql = 'UPDATE {テーブル名} SET column_1=?,column_2=? WHERE column_0=?';
$stmt = $dbh -> prepare($sql);
$data[] = $string_1;
$data[] = $string_2;
$data[] = $int_0;
$stmt -> execute($data);
$dbh = null;
DELETE(行を削除)
$int_0 = '{なんかの数字}';
$sql = 'DELETE FROM {テーブル名} WHERE code=?';
$stmt = $dbh -> prepare($sql);
$data[] = $int_0;
$stmt -> execute($data);
$dbh = null;
コメントを残す