2

The following pg_query() calls trigger warnings:

// with an active and working default connection:
$res = pg_query("");
$res = pg_query(";");
$res = pg_query("// foo\n; ;\n /* bar */\n ; // baz");

The warning is "Warning: pg_query(): Query failed: in {script} on line {n}".

In all three cases, $res is false, signalling a failure, but pg_last_error() is empty. The obvious cause is that the query string is empty (after removing comments and semicolons).

This is the only situation where I've ever seen a false return value of pg_query() without an error message in pg_last_error(). I would like to log a message with the actual reason for the failure for debugging purposes. Can I rely on empty query strings being the only cause of this combination? Is there any other source of information about the failure, beside pg_last_error()?

Zilk
  • 8,917
  • 7
  • 36
  • 44
  • Try calling `pg_last_error()` after connecting to the database, your issue may be that there is no connection for whatever reason. `pg_last_error()` will be empty if there's no connection to the database. It's only useful after you connect. – BugHunterUK Feb 08 '16 at 11:54
  • The connection exists and works; I have left that part out for brevity. Will fix. – Zilk Feb 08 '16 at 11:58
  • Does your query work when running it directly from Postgresql? A very odd situation indeed. – BugHunterUK Feb 08 '16 at 12:03
  • I'm not sure I understand your question... there is no query. I can evaluate `;` in psql, yes, but that's just an empty statement. – Zilk Feb 08 '16 at 12:06
  • As far as PostgreSQL is concerned the empty string `''` is a perfectly valid thing to send to the backend. It is not an error but does not produce any result set, command tag or affected row count. On the protocol level it returns to ReadyForQuery state. – Craig Ringer Feb 08 '16 at 12:10
  • That explains the empty result and absence of errors, thank you. Is there any other way to produce this combination, apart from an empty query? – Zilk Feb 08 '16 at 23:31
  • I'm getting the same result (FALSE result but empty pg_last_error()) on actual errors, as described [here](http://stackoverflow.com/questions/42118755). No idea why (yet), but it may be relevant to what you're asking. – Joe Strout Feb 08 '17 at 18:06

0 Answers0