### 接続
$dsn = "pgsql:dbname=testdb host=localhost port=5432"; $user = "user1"; $password = "password"; try { $dbh = new PDO($dsn, $user, $password); print('接続に成功しました'); } catch(PDOException $e){ print("Error:".$e->getMessage()); die(); }
### sql実行
$dsn = "pgsql:dbname=testdb host=localhost port=5432"; $user = "user1"; $password = "password"; try { $dbh = new PDO($dsn, $user, $password); print('接続に成功しました<br>'); $sql = 'select * from department'; foreach($dbh->query($sql) as $row){ print($row['department_code'].":"); print($row['department_name'].","); } } catch(PDOException $e){ print("Error:".$e->getMessage()); die(); }
$ php index.php
接続に成功しました
a:営業部,b:総務部,d:経理部,e:人事部,f:物流部,
PDOはmysqlと然程変わらないですな