为什么mysql_query()用SELECT语句返回TRUE? [英] Why does mysql_query() return TRUE with a SELECT statement?
问题描述
根据 mysql_query()
的手册以及我所知道的关于此功能的所有知识,我已经使用了很多次,它可以返回资源,如果查询是SELECT
,则可以返回FALSE
.但是它会不时返回TRUE
.
According to the manual of mysql_query()
and to everything I know about this function that I used so many times, it can either return a resource or FALSE
if the query is a SELECT
. Yet it returns TRUE
from time to time.
怎么可能?它从来没有发生过.这是PHP 5.3.2中的错误吗?有人知道吗?
How can this be? It never ever happened before. Is this a bug in PHP 5.3.2? Does anyone know anything about this?
代码类似于:
if (!$resource = mysql_query($query, $handle)) {
throw some exception;
}
var_dump($query);
if ($resource === true && strpos($query, 'SELECT') !== false) {
throw new Exception('mysql_query() returned TRUE for SELECT');
}
也很难复制.它只会不时发生.我还注意到,这很可能是在服务器突然中断连接的同时发生的,在这种情况下,它应该返回FALSE
...
It's pretty hard to reproduce, too. It happens only from time to time. I also noticed that it's likely this happens at the same time the server interrupts the connection suddenly, in which case it should return FALSE
...
推荐答案
如果webbiedave不在正确的轨道上,则在php源中只有一种代码路径可以解决这种情况:
If webbiedave isn't on the right track, there's only one codepath that allows for this situation in the php source:
#if MYSQL_VERSION_ID < 32224
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_num_fields(mysql)>0)
#else
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_field_count(mysql)>0)
#endif
...
if (!mysql_result) {
if (PHP_MYSQL_VALID_RESULT(mysql->conn)) { /* query should have returned rows */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
RETURN_FALSE;
} else {
RETURN_TRUE; // <<< this case
}
}
我认为这是一个错误.尤其是因为没有真正的方法可以验证这一点-PHP代码中的mysql_num_fields使用的是您没有得到的资源,而不是连接.
I would consider this a bug. Especially since there's no real way to verify this - mysql_num_fields in the PHP code uses the resource that you're not getting, not the connection.
尽管仍然很奇怪,mysql_query的C版本在失去连接时返回零-如果可以,请尝试以下补丁并重新安装mysql扩展:
Although it's still weird that the C version of mysql_query returns zero on lost connection - if you're able to, try the following patch and reinstall the mysql extension:
Index: ext/mysql/php_mysql.c
===================================================================
--- ext/mysql/php_mysql.c (revision 311719)
+++ ext/mysql/php_mysql.c (working copy)
@@ -1485,6 +1485,9 @@
if (PHP_MYSQL_VALID_RESULT(mysql->conn)) { /* query should have returned rows */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
RETURN_FALSE;
+ } else if( mysql_errno(mysql->conn) != 0 ) {
+ php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
+ RETURN_FALSE;
} else {
RETURN_TRUE;
}
这篇关于为什么mysql_query()用SELECT语句返回TRUE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!