Re: pg_result_error and tests/large_object.inc
| From: | Yasuo Ohgaki | Date: | Thu, 02 May 2002 00:15:33 +0000 |
| Subject: | Re: pg_result_error and tests/large_object.inc | ||
| References: | 1 | Groups: | php.qa |
| Request: | Send a blank email to php-qa+get-5294@lists.php.net to get a copy of this message | ||
Melvyn Sopacua wrote:
Hi,
it's not entirely clear to me, how pg_result_error should be used.
When I make an illegal query, like:
<?php
$con=pg_connect('host=127.0.0.1 dbname=pbase user=test password=test');
$sql="SELECT id FROM non_existent";
$rs=pg_query($con, $sql);
if(!$rs)
{
echo pg_result_error($rs);
echo "not ";
}
echo "OK";
pg_close($con);
?>
I'm presented with:
PHP Warning: pg_result_error() expects parameter 1 to be resource, boolean given
pg_result_error() is used to check error in result resource.
Since pg_query() return FALSE(boolean) for invalid query. Therefore,
it is working as expected.
With pg_send_qeury(), user can send multiple queries at once,
then retrieve query result one by one via pg_get_result().
pg_last_error() does not work well especially with this usage.
Which is kind of logical. The sync_query.inc in tests/ is the only file it's used, obviously on a valid query, so that doesn't make it much clearer. Yasuo: could you make an example of how this should be used?<?php $db = pg_connect(); $sql = "select * from table1; select * from table2"; pq_send_query($db, $sql) or die("failed"); // dump all while($result = pg_get_result($db)) { for(;$result && ($data = pg_fetch_array($result));) {
!pg_result_error($result) or die('result error');
var_dump($data);
}
}
?>
pg_result_error() is especially useful for long life applications
like GTK application or stricter consistency checks are needed for
mission critical applications.
For most web scripts, pg_result_error() may be too much.
Another example,
<?php
$db = pg_connect();
$sql = "select * from table1; select * from table2";
pq_send_query($db, $sql) or die("failed");
// dump all
$result1 = pg_get_result($db);
// do something
$result2 = pg_get_result($db);
// do something
pg_last_error($db);
// we don't know error is accociated with result1 or result2
// with pg_last_error(). We need pg_result_error() to
// know better error info.
?>
Secondly: tests/large_object.inc uses plain "host=localhost" as connection string. I personally don't see a reason for that, so could you change that? $ diff -u large_object.inc.dist large_object.incThank you for let me know. It seems it's fixed already in CVS version. -- Yasuo Ohgaki--- large_object.inc.dist Mon Dec 17 01:16:48 2001 +++ large_object.inc Wed May 1 20:12:24 2002@@ -2,7 +2,7 @@ include('config.inc'); -$db = pg_connect("host=localhost"); +$db = pg_connect($conn_string); pg_exec ($db, "begin"); $oid = pg_lo_create ($db); if (!$oid) echo ("pg_lo_create() error\n");