根据 mysql_query()
的 PHP 文档:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
如果 SELECT 在成功时返回资源,它实际上在成功时返回 TRUE 吗?那么这样做仍然有效吗?:
<?php
$result = mysql_query('SELECT * WHERE 1=1');
if ($result) {
//resultset is valid?
}
?>
我几乎可以肯定它确实有效,但是它是如何工作的呢? mysql_query() 只返回几个属性吗?
最佳答案
来自PHP manual entry on booleans :
When converting to boolean, the following values are considered FALSE:
- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string, and the string "0"
- an array with zero elements
- an object with zero member variables (PHP 4 only)
- the special type NULL (including unset variables)
- SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
重点是我的,但也适用于您的情况。
此外,请勿在代码中使用 mysql_*
函数。这些功能不再维护,并且是 being deprecated 。相反,您应该使用 MySQLi或PDO 。不知道该使用哪个? This article应该有帮助。
关于php - PHP 中的资源等于 true 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13389688/