对了,$dsn = pgsql://postgres:[email protected]:8848/testdb
这样是不能连接成功的。

解决方案 »

  1.   

    应该这样:
    $dsn = pgsql://postgres:123@tcp+192.168.0.1:8848/testdb
      

  2.   

    pg_connect
    (PHP 3, PHP 4 )pg_connect -- 打开一个 PostgreSQL 连接
    说明
    resource pg_connect ( string connection_string)
    pg_connect() 返回其它 PostgreSQL 函数所需要的资源。 pg_connect() 打开一个由 connection_string 所指定的 PostgreSQL 数据库的连接。如果成功则返回连接资源,如果不能连接则返回 FALSE。connection_string 应该是用引号引起来的字符串。 例子 1. 使用 pg_connect()<?php
    $dbconn = pg_connect("dbname=mary");
    //connect to a database named "mary"
    $dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");
    // connect to a database named "mary" on "localhost" at port "5432"
    $dbconn3 = pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo");
    //connect to a database named "mary" on the host "sheep" with a username and password$conn_string = "host=sheep port=5432 dbname=test user=lamb password=bar";
    $dbconn4 = pg_connect($conn_string);
    //connect to a database named "test" on the host "sheep" with a username and password
    ?>  
     
    connection_string 所包括的参数有 host,port,tty, options,dbname, user 和 password。 如果用同样的 connection_string 再次调用 pg_connect(),不会建立新连接,而是返回前面已经打开的连接资源。如果使用不同的连接字符串,则可以和同一个数据库建立多个连接。 旧的多参数语法 $conn = pg_connect("host", "port", "options", "tty", "dbname") 已经不提倡使用。
      

  3.   

    多谢mistjin(9527)  及各位的回答,
      

  4.   

    $dsn = pgsql://postgres:[email protected]+8848/testdb
    当然你必须先设置好端口啦
    一般来说,$dsn是一个字符串,它的格式如下: 
    phptype(dbsyntax)://username:password@protocol+hostspec/database     *  phptype:  php后端数据库的类型名称(如mysql, odbc 等等.)
         *  dbsyntax: 数据库所使用的SQL语法标准,一般不用。
         *  protocol: 使用的通讯协议。(如tcp, unix 等等.)
         *  hostspec: 数据库所在的主机的描述。(形式是:主机名[:端口号])
         *  database: 数据库的名称。
         *  username: 登陆的用户名。
         *  password: 登陆的密码。
    对于DSN,常用的形式如下: 
         *  phptype://username:password@protocol+hostspec:110//usr/db_file.db
         *  phptype://username:password@hostspec/database_name
         *  phptype://username:password@hostspec
         *  phptype://username@hostspec
         *  phptype://hostspec/database
         *  phptype://hostspec
         *  phptype(dbsyntax)
         *  phptype
    对于省略的部分,将使用缺省值。当然,$dsn也可以是一个数组,数组的形式如下: 
    $dsn = array(
           'phptype'  => 'mysql',
           'dbsyntax' => '',
           'protocol' => '',
           'hostspec' => 'localhost',
           'database' => 'test',
           'username' => 'root',
           'password' => ''
           )
      

  5.   

    感谢xuzuning(唠叨)的回复,但你说的这样连接:
    $dsn = pgsql://postgres:[email protected]+8848/testdb
    好象是不行的,
    而按照 mistjin(9527)说的
    $dsn = pgsql://postgres:123@tcp+192.168.0.1:8848/testdb
    可以连
      

  6.   

    那就得根据你的pear版本来决定了
    你看看db.php中parseDSN方法的处理过程就知道该怎么写了