dio_bless
dio_bless

Reputation: 475

"php fatal error" using pg_connect

I have installed php5 and php5-pgsql on my ubuntu(linux mint). And i can not get result of query in php script:

$db = pg_connect("host=localhost port=5432 dbname=test user=user")
or die("Could not connect: test db\n"); 
$part = $db->query("SELECT * FROM app_settings");

This query works right, running in pg_admin and all db connection attributes are right!

But i got this error:

PHP Fatal error:  Call to a member function pg_query() on a non-object in

What's the problem, help pls!

Upvotes: 0

Views: 1196

Answers (2)

Panama Jack
Panama Jack

Reputation: 24448

Try enabling pgsql in PHP.ini file

add this line in the extensions

extension=pgsql.so

Update:

Not sure that is right. What about this.

$db = pg_connect("host=localhost port=5432 dbname=test user=user")
or die("Could not connect: test db\n"); 
$part = pg_query($db, "SELECT * FROM app_settings");

while ($row = pg_fetch_array($part)) {
 //do stuff

}

Upvotes: 0

Gerald Schneider
Gerald Schneider

Reputation: 17797

You are mixing pg_* functions and PDO. pg_connect() does not return an object, it returns a connection resource for later use in pg_query(). You can do it either way:

$res = pg_connect(...);
$result = pg_query($res, "SQL query"); // where $res is optional if you only use one connection at a time
while ($row = pg_fetch_row($result)) {
  // ...
}

or you use PDO

$db = new PDO("...");
$db->query("SQL query");
// ...

I'd prefer PDO with prepared statements.

Upvotes: 1

Related Questions