Checking pg_query_params return value with PHP

How can you check if a resource is false in PHP pg request parameters ?

I run the following code unsuccessfully

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

He 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 $dbconnwhat is right, so the second warning seems like only a symptom. The reason for this is an invalid integer type.

My request is

$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 )
);
0
source share
2 answers

Try the following:

$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 using $ result, you need to check it.

+3
source

"NULL-", , , .

, , , - . , , pg_affected_rows (UPDATE/INSERT/DELETE) pg_num_rows (SELECT).

+1

Source: https://habr.com/ru/post/1716388/


All Articles