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';}
>
测试成功 !

解决方案 »

  1.   

    <?php
    /****************************************************************************************************
    函数功能: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; 
        } 

    ?>
      

  2.   

    <?php
      $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(); 
    ?>
      

  3.   

    pg_errormessage(): supplied argument is not a valid PostgreSQL link resource in /usr/local/lib/pear/DB/pgsql.php应该是pear本身的错误,而大意上是说,无效的PostgreSQL连接。
      

  4.   

    怎么测试pear 的错误呢
    重新安装pear?