Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

postgresql - Checking return value of pg_query_params by PHP

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 )
);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...