user1140240
user1140240

Reputation: 639

php pg_query not working?

I'm very new to php and Postgresql. When I was trying to retrieve data from my database, the query seems to always return a FALSE value. Here is my code:

$dbconn = pg_connect("host=localhost port=5432 dbname=mydb user=user password=user")
if(!$dbconn) {
    echo "Not connected!";
}
$sql = 'SELECT * FROM "mytable"';
$query = pg_query($dbconn,$sql);
if(!$query) {
    echo "There is an error!";
    echo pg_last_error($dbconn);
}

Since the "not connect!" is not printed out, I'm quite sure my connection is correct. However, "There is an error!" is printed out, but pg_last_error() gives me an empty string.

Is there a reason why an empty string is passed back and is there other ways to retrieve the error message? Thanks a lot!

Upvotes: 1

Views: 3505

Answers (2)

Silver Light
Silver Light

Reputation: 45902

Everything seems correct, so I would do the following:

  1. Make sure that "mytable" exists and has some data
  2. Put another pg_error after connecting to database:

e.g.

$dbconn = pg_connect("host=localhost port=5432 dbname=mydb user=user password=user");
var_dump($dbconn); // will it print "resource"?
echo pg_last_error($dbconn);

Upvotes: 3

Fred FLECHE
Fred FLECHE

Reputation: 452

can you try to change :

$sql = 'SELECT * FROM "mytable"';

By

$sql = "SELECT * FROM mytable";

It should fix your problem

Upvotes: 1

Related Questions