9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP PDO PostgreSQL 接続サンプル

Last updated at Posted at 2015-12-18

PHPのPDOにて、PostgreSQLにアクセスするサンプルプログラム
失敗時は例外処理を実施する

DB_PDO_sample.php
<?php
$dsn = 'pgsql:dbname=dbname_1 host=db.host.com port=5432';
$user = 'postgres_user';
$password = 'db_passsword';
try{
	$dbh = new PDO($dsn, $user, $password);
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $query =<<<__QUERY__
 SELECT :test ;
__QUERY__;
        $stmt = $dbh->prepare($query,array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $array = array(':test'=>' aaaa ');
        $stmt->execute($array);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}catch (PDOException $e){
	print $query;
	print('Error:'.$e->getMessage());
	die();
} catch (Exception $e) {
	print('Error:'.$e->getMessage());
	die();
}

9
11
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
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?