「PHP」PostgreSQLへ接続するサンプル
書式
new PDO(dsn名,ユーザ名,パスワード)
使用例
<?php
// DB接続情報
$dsn = 'pgsql:host=172.17.2.110;dbname=testdb';
$username = 'root';
$password = '12345#a';
$userno = 1001;
try{
// PDOインスタンス
$pdo = new PDO($dsn,$username,$password);
// select構文 プレースホルダuserno
$cftSQL = "select * from user_tbl where userno = :userno";
$stmt = $pdo->prepare($cftSQL);
// 変数をバインド
$stmt -> bindParam(":userno",$userno);
$stmt -> execute();
// データを取得する
$rec = $stmt->fetch(PDO::FETCH_ASSOC);
}catch (PDOException $e) {
//some code
}
<?php
// DB接続情報
$dsn = 'pgsql:host=172.17.2.110;dbname=testdb';
$username = 'root';
$password = '12345#a';
$userno = 1001;
try{
// PDOインスタンス
$pdo = new PDO($dsn,$username,$password);
// select構文 プレースホルダuserno
$cftSQL = "select * from user_tbl where userno = :userno";
$stmt = $pdo->prepare($cftSQL);
// 変数をバインド
$stmt -> bindParam(":userno",$userno);
$stmt -> execute();
// データを取得する
$rec = $stmt->fetch(PDO::FETCH_ASSOC);
}catch (PDOException $e) {
//some code
}
<?php // DB接続情報 $dsn = 'pgsql:host=172.17.2.110;dbname=testdb'; $username = 'root'; $password = '12345#a'; $userno = 1001; try{ // PDOインスタンス $pdo = new PDO($dsn,$username,$password); // select構文 プレースホルダuserno $cftSQL = "select * from user_tbl where userno = :userno"; $stmt = $pdo->prepare($cftSQL); // 変数をバインド $stmt -> bindParam(":userno",$userno); $stmt -> execute(); // データを取得する $rec = $stmt->fetch(PDO::FETCH_ASSOC); }catch (PDOException $e) { //some code }