postgresql 学习

前端之家收集整理的这篇文章主要介绍了postgresql 学习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ref http://PHP.net/manual/zh/book.pgsql.PHP

1.install

cd PHP-5.4.36/ext/pgsql/

PHPize

./configure

make && make install

addextension=pgsql.so


2. test

//Pgsql.PHP

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

猜你在找的Postgre SQL相关文章