php - PHP 中的资源等于 true 吗?

标签 php

根据 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 。相反,您应该使用 MySQLiPDO 。不知道该使用哪个? This article应该有帮助。

关于php - PHP 中的资源等于 true 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13389688/

相关文章:

javascript - 使用javascript函数执行php文件

php - php中这两个语句有什么区别?

php - Zend Framework::忽略特定操作的预调度

php - 打包 PHP-MySQL 应用程序以便于安装

php - 计算并限制我的应用程序的用户数量

PHP Julian 日期转换器算法

php - 在 php 中将列表更改为嵌套对象的最有效方法是什么

php - 如何使用 codeigniter 查询获取列的总和

php - Paypal IPN 没有响应,只有 "Connection Close"

php - 如何通过将数字添加到数据库中的预存数字来更新记录