Warning: pg_errormessage(): supplied argument is not a valid PostgreSQL link resource in /usr/local/lib/pear/DB/pgsql.php on line 410
是不是没有连接上呀
但是我用
<?php
require_once 'DB.php';$dsn = array(
'phptype' => 'pgsql',
'username' => 'xxxxxxxx',
'password' => 'xxxxxxxxxx',
'hostspec' => 'xxxx.xxxx.xxxx',
'database' => 'xxxxx',
'port' => '5433'
);$options = array(
'debug' => 2,
'portability' => DB_PORTABILITY_ALL,
);$db =& new DB;
$conn=$db->connect($dsn, $options);
if (PEAR::isError($db)) {
die($db->getMessage());
}else{echo 'dbconnect ok';}
>
测试成功 !
是不是没有连接上呀
但是我用
<?php
require_once 'DB.php';$dsn = array(
'phptype' => 'pgsql',
'username' => 'xxxxxxxx',
'password' => 'xxxxxxxxxx',
'hostspec' => 'xxxx.xxxx.xxxx',
'database' => 'xxxxx',
'port' => '5433'
);$options = array(
'debug' => 2,
'portability' => DB_PORTABILITY_ALL,
);$db =& new DB;
$conn=$db->connect($dsn, $options);
if (PEAR::isError($db)) {
die($db->getMessage());
}else{echo 'dbconnect ok';}
>
测试成功 !
解决方案 »
- PHP链接SQL Server数据库?
- 为什么用了Smarty之后.....
- 如何每一个option触发一个action?
- 求助:关于使用cURL函数读取https协议页面的问题
- 求一个smtp发送mail的代码(需要支持HTML,用不用类均可)
- 帮忙!关于设置的问题!
- 头疼问题:我的session还是不能用,急死了!
- 请教一下php文件之间怎样传值?
- 限制手机短信验证发送次数
- PHP连接mysql时出现Fatal error: Call to undefined function mysql_connect()
- 大家帮我听听这验证码是什么?放
- PEAE连接PGSQL出错大家帮忙看看什么问题?
/****************************************************************************************************
函数功能:Postgresql 数据库操作函数
范例:
PostgreSQL 8.* Simple Usage Class
$db = & new PostgreSQL();
$record = & $db->query_first("SELECT * FROM table WHERE id=xxx"); //single record:
//many lines:
if ($records = & $db->query_all("SELECT * FROM table WHERE id>xxx")) {
foreach ($records as &$record) {
...
}
}
*****************************************************************************************************/
class PostgreSQL {
private $pconnect = 1;
private $querynum = 0;
private $dbconnect;
private $db_char='gbk';
function __construct($pconnect=1) { //构造函数
$this->pconnect = $pconnect;
$this->connect();
}
function connect()
{
$connect = "host=" .db_hostname. " port=" .db_port. " dbname=" .db_database. " user=". db_user;
if (db_password!='')
$connect.=" password=".db_password;
if ($this->pconnect):
$this->dbconnect=pg_pconnect($connect);
else:
$this->dbconnect=pg_connect($connect);
endif;
if (!$this->dbconnect):
$this->halt("Cannot connect to database!|无法连接到数据库!");
else:
//echo '链接成功!';
endif;
}
function query($sql, $silence = 0)
{ //应用于insert update select[一般是能执行单条命令]
$setchars="set names '". $this->db_char ."'";//设置数据编码
pg_query($this->dbconnect, $setchars);
$result = pg_query($this->dbconnect, $sql);
if(!$result && !$silence) {
$this->halt('PgSQL Query Error/PgSQL 查询错误', $sql);
}
$this->querynum++;
return $result;
}
// does a query and returns first row by reference
function & query_first($sql, $result_type = PGSQL_ASSOC)
{
$result = $this->query($sql);
$returnarray=@ pg_fetch_array($result, 0, $result_type);
$this->free_result($result);
return $returnarray;
}
function insert_id($table,$ID="id")
{//这个函数用于取得刚插入行的id
$sql="select $ID from ". $table ." order by $ID desc ";
$inrs=$this->query_first($sql);
$teturnId=$inrs[$ID];
return $teturnId;
}
function pg_insert_id($tablename, $fieldname="id")
{ //这个函数用于取得刚插入行的id
$sql="SELECT last_value FROM ${tablename}_${fieldname}_seq";
$result = $this->query($sql);
$seq_array=pg_fetch_row($result, 0);
return $seq_array[0];
}
function & query_all($sql, $result_type = PGSQL_ASSOC)
{
$result = $this->query($sql);
$returnarray = (function_exists("pg_fetch_all"))
? pg_fetch_all($result)
: $this->fetch_all($result, $result_type);
$this->free_result($result);
return $returnarray;
}
/*------------------------------------------------------------------------
result_type 是一个常数,
可以有以下取值∶PGSQL_ASSOC,PGSQL_NUM 和 PGSQL_BOTH。
取值为 PGSQL_ASSOC 时 pg_fetch_array() 传回用字段名作为键值索引的关联阵列,
取值为 PGSQL_NUM 时用字段编号作为键值,
取值为 PGSQL_BOTH 时则同时用两者作为键值。
预设值是 PGSQL_BOTH。
------------------------------------------------------------------------*/
function fetch_array($result, $row, $result_type = PGSQL_ASSOC)
{
return @ pg_fetch_array($result, $row, $result_type);
}
function fetch_object($result) {
return @ pg_fetch_array($result);
}
function fetch_all($result)
{ //传回多条记录 应用于SELECT 查询
while ($row = pg_fetch_assoc($result))
{
$array_out[] = $row;
}
return $array_out;
}
function affected_rows()
{ //传回受影响的记录数 return pg_affected_rows();
}
function error()
{//传回数据库出错信息
return pg_last_error();
}
function num_rows(&$query) {// 返回行的数目
return pg_num_rows($query);
}
function num_fields(&$query)
{
return pg_num_fields($query);
}
function free_result(&$query)
{
return pg_free_result($query); //释放查询结果占用的内存
}
function fetch_row(&$query)
{ // 提取一行作为枚举数组
return pg_fetch_row($query);
}
function __destruct()
{ //析构函数 关闭数据
if (!$this->pconnect)
return pg_close();
}
function halt($message = '', $sql = '') {
$timestamp = time();
$errmsg = '';
$dberror = $this->error();
if($message) {
$errmsg = "<b>info</b>: $message\n\n";
}
$errmsg .= "<b>Time</b>: ".gmdate("Y-n-j g:ia", $timestamp)."\n";
$errmsg .= "<b>Script</b>: ".$_SERVER['PHP_SELF']."\n\n";
$errmsg = nl2br($errmsg);
if($sql) {
$errmsg .= "<!--\n\nSQL: $sql\n";
}
$errmsg .= "Error: $dberror\n\n-->";
echo "<p>$errmsg</p>";
exit;
}
}
?>
$charset="gb2312"; //网站编码;
define("DOC_ROOT",realpath(dirname(__file__)."/../")."/");
define("db_user","postgres"); //数据库用户名
define("db_hostname","localhost"); //数据库服务器地址
define("db_port","5432"); //端口号
define("db_database","databasename"); //数据库名
require_once("DB.php"); //mysql数据库操作 类
$db=new PostgreSQL();
?>
重新安装pear?