ref http://PHP.net/manual/zh/book.pgsql.PHP
1.install
PHPize
./configure
make && make install
addextension=pgsql.so
2. test
<?PHP
define("HOST","127.0.0.1");define("PORT",5432);
define("DBNAME","test");
define("USER","test");
define("PASSWORD","test");
class Pgsql {
//单例
private static $instance = null;
private $conn = null;
private function __construct()
{
$this->conn = pg_connect("host=" . HOST . " port=" . PORT . " dbname=" . DBNAME . " user=" . USER . " password=" . PASSWORD) or die('Connect Failed : '. pg_last_error());
}
public function __destruct()
{
pg_close($this->conn);
}
/**
* 单例模式
* @param $name
*/
public static function getInstance()
{
if ( ! self::$instance )
{
self::$instance = new self();
}
return self::$instance;
}
/**
* 获取记录
*/
public function fetchRow($sql)
{
$ret = array();
$rs = pg_query($this->conn,$sql);
$ret = pg_fetch_all($rs);
if ( ! is_array($ret) )
{
return array();
}
return $ret;
}
/**
* 执行指令
* @param string $sql
*/
public function query($sql)
{
$result = pg_query($this->conn,$sql);
if ( ! $result )
die("sql : {$sql} " . pg_last_error());
}
/**
* 获取一条记录
*/
public function fetchOne($sql)
{
$ret = array();
$rs = pg_query($this->conn,$sql);
$ret = pg_fetch_all($rs);
if ( ! is_array($ret) )
{
return array();
}
return current($ret);
}
/**
* add one record
*/
public function add($data,$table) {
return pg_insert($this->conn,$table,$data);
}
}
// test.PHP
<?PHP include "./Pgsql.PHP"; $pg = Pgsql::getInstance(); var_dump($pg); $ret = $pg->fetchRow("select * from test"); var_dump($ret); $data = array('id'=>33,'text'=>'testdata'); $ret = $pg->add($data,'test'); var_dump($ret);
原文链接:https://www.f2er.com/postgresql/195408.html