加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql 学习

发布时间:2020-12-13 17:22:53 所属栏目:百科 来源:网络整理
导读: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"); de

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读