简体   繁体   English

PHP / PSQL / SQL资源是否总是在成功查询后返回?

[英]PHP/PSQL/SQL Does a resource always return on successful query?

I am writing some business critical queries and am relying on is_resource to filter all failed queries (ie, bad syntax etc.) 我正在编写一些关键业务查询,并依靠is_resource筛选所有失败的查询(即语法错误等)。

Take this query for example: 以以下查询为例:

$result = pg_query('SELECT * FROM logs');

if (!is_resource($result)) {
    die('Error');
}

Will that also die('Error') if the query works but no rows are returned? 如果查询有效但不返回任何行,那还会die('Error')吗? I need it to return a resource even if no rows have been returned from the database. 即使没有从数据库返回任何行,我也需要它来返回资源。

There will be a resource if there is no error. 如果没有错误,将有一个资源。 To check rows, you want to use pg_num_rows . 要检查行,您想使用pg_num_rows

Quote from php.net: "If an error occurs, and FALSE is returned, details of the error can be retrieved using the pg_last_error() function if the connection is valid." 引自php.net:“如果发生错误,并且返回FALSE,则在连接有效的情况下,可以使用pg_last_error()函数检索错误的详细信息。” "A query result resource on success or FALSE on failure." “成功的查询结果资源,或失败的查询结果资源。”

That sounds like there will always be a resource as result as long as there was no error. 听起来只要没有错误,就总会有资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM