|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #7141 pg_exec returns true even though it failed
Submitted: 2000-10-11 17:26 UTC Modified: 2000-10-28 00:01 UTC
From: dbi at xmission dot com Assigned:
Status: Closed Package: PostgreSQL related
PHP Version: 4.0.2 OS: Redhat Linux 6.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: dbi at xmission dot com
New email:
PHP Version: OS:


 [2000-10-11 17:26 UTC] dbi at xmission dot com
When I execute the following code segment with the $EmailAddress and $Password variables set to values that exist in the PostgreSQL 6.5.3 table, everything works fine.

When I execute it with values that are NOT in the table, pg_exec still returns a "true" value even though the SELECT statement never found any matching rows.  (This is contrary to what the documentation implies when it tests for !$result.)

The actual value of the $query variable, even when the query sent by the pg_exec function fails, is "Resource id #2".  Because it's not falst, there's no way to test whether the query succeeded.  So when the program goes on to execute the pg_fetch_array function, the $query variable points to nothing and the program dies with an error message.


// -- This module checks a username and password to see who the user is.)
$db = pg_connect("", "", "", "", "tracker");
if ($db) {
    $SQLQuery = "SELECT userid FROM users WHERE username = '" . $EmailAddress . "' AND password = '" . $Password . "';";
    $query = pg_exec($db, $SQLQuery);
    if ($query) {
        $row = 0;
        if ($data = pg_fetch_array($query, $row)) {
            // Etc...


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2000-10-28 00:01 UTC]
I agree that the docs could be a bit more clear about the return value of
pg_exec(). But this is *not* a bug. Even a SELECT that doesn't return any rows
still succeeds and returns a resource id. You'll have to test whether it returned
any rows with pg_numrows(). See the example of pg_fetch_row() in manual.
pg_exec() returns FALSE in the case that the query couldn't be executed at all,
for example because it was malformed or the PostgreSQL server itself encountered
some other error (no privileges to select from a table etc.).
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 19 08:01:28 2024 UTC