tags:

views:

191

answers:

2

How can you check if a resource is false in PHP`s pg query params?

I run the following code unsuccessfully

 $row = pg_num_rows ( $result );
 if ( $row !== 0 )
        echo "hurray";
 else
        echo "argh";

It gives me the following warnings

Warning: pg_query_params() [function.pg-query-params]: Query failed: ERROR: invalid input syntax for integer: "" in /var/www/codes/index.php on line 120

I use $dbconn which is correct so the the second warning seems to be only a sympton. The cause of this is the invalid type for an integer.

My query is this

$dbconn = pg_connect("host=localhost port=5432 dbname=masi user=masi password=123");
$result = pg_query_params ( $dbconn,
    'SELECT question_id
    FROM questions
    WHERE question_id = $1',
    array ( $question_id )
);
+2  A: 

Try this:

$result = @pg_query_params($dbconn, "SELECT * FROM SOME_TABLE", array());
if($result !== FALSE)
{
  $row = @pg_num_rows($result);
  if ( $row !== 0 )
         echo "hurray";
  else
         echo "argh";
}
else
{
  echo "Seems, your query is bad.";
}

Before you use $result, you need check it.

Vitaly Dyatlov
Although using !== to check for FALSE is generally a good idea, in this case it doesn't really matter. The query result will never be any other kind of false than boolean FALSE.
Inshallah
**What is the purpose of @ in your commands?**
Masi
@ - prevent error/warning/notice reports
Vitaly Dyatlov
This `if($result !== FALSE)` -clause is extraneous, since it is never true for me although it should because I can see the data out of my database.
Masi
Sure, you can see data out from your database, but if $result for you is FALSE, then your query syntax is wrong. Is it possible to show your query to me?
Vitaly Dyatlov
@Vitaly: I added the query to my question.
Masi
Your query must be single quoted, because with double quotes php parsing $1, that's right:$result = pg_query_params ( $dbconn, 'SELECT question_id FROM questions WHERE question_id = $1', array ( $question_id ));
Vitaly Dyatlov
I get still the same error message. The if clause `if ( $result !== FALSE )` is still always true.
Masi
This answer http://stackoverflow.com/questions/1351268/or-in-phps-postgres-queries/1351281#1351281 suggests me that I should apparently use double quotes in the query.
Masi
$question_id consists right integer value?
Vitaly Dyatlov
For request with params to Postgre you need use single quotes.http://md.php.net/manual/en/function.pg-query-params.php
Vitaly Dyatlov
The problem is now solved. I had a missing `!` in my one of my if clauses. Thank you for your help!
Masi
+1  A: 

When you say "NULL query" I assume that you mean a query that does not affect or return any result rows.

Any successful query will always result in a valid query result resource, which will always be true, whether or not any rows have been affected/returned. To see whether the query was successfully applied to one or more rows you have to use either pg_affected_rows (UPDATE/INSERT/DELETE) or pg_num_rows (SELECT).

Inshallah
I found a bug in my code about the use of `pg_num_rows`. Please, see my updated question.
Masi
I run the query, `SELECT * FROM questions`, in Psql and works fine. - The problem is not in the query in my opinion.
Masi