LoginSignup
2
2

More than 5 years have passed since last update.

PDOの使い方

Posted at

phpでPDOを使ったので忘れないようにメモ。
PDOってそもそも何よ?
→PHP Data Objects の略で、「PHP の中からデータベースにアクセスするための軽量で高性能な インターフェイスを定義します」らしい。

■まずはDBへの接続方法(MySQLの場合)
$dbh = new PDO('mysql:host=ホスト名;dbname=DB名', ユーザ名, パスワード);
これでPDO基底クラスのインスタンスが作成される。
※DBへの接続を閉じる場合は、$dbhにnullを入れてやればよい。

■で、DB操作するにはどうするの?
こんな感じでSQL文を作って実行する。
 $sql = 'select dataname from table where ? is ?';
$sth = $dbh->prepare($sql);
$sth ->execute(array('query','foo'));
$result = $sth->fetchAll();

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