php pg_query()不工作?

时间:2014-12-21 20:59:26

标签: php postgresql

我对Postgresql很新。这个错误是什么?

警告:pg_query()期望参数1为资源,在第25行的C:\ xampp \ htdocs \ torf.php中给出null

class user {

private $link=null; 

private function connect() {
    if ($this->link == null || !pg_ping($this->link) ) {
        $this->link = pg_connect("host=localhost port=5432 dbname=bigchance user=postgres password=lamp");
    }
}

public function user() {
    $this->connect();
}

public function new_id() {
    $id=0;
    $result = pg_query (user::connect(), "select * from user order by id desc limit 1 " );
    if ($result != false && pg_num_rows($result>0))  {
        $id= pg_fetch_result($result,0,0);
    }
    $id++;
    return $id;
}

public function insert( $email,$password ) {
    $id=$this->new_id();
    $result=pg_query(user::connect(),"insert into user (id,email,password,ip,created_td) values ('2','{$email}','{$password}','0','0')  ");
    return pg_affected_rows($result);
}
}

我正在使用$this->connect(),但出现了错误:(

1 个答案:

答案 0 :(得分:1)

您正尝试将connect函数的结果用作参数:pg_query (user::connect(), ...,但它没有return语句,因此将评估为null

您需要拨打$this->connect()一次(您已在构造函数中执行此操作),然后传递$this->link,如pg_query($this->link, ...