如果没有行返回那么当使用 php 和 mysql 时它是 false
我注意到很多教程指令经常有这样的代码:
$sql="SELECT * from table";
$num=mysql_num_rows();
if ($num > 0)
{
do something
}
当我编写我认为紧凑且可读的代码时,为什么他们必须使用条件“if($num > 0)”:
$sql="SELECT * from table";
$itExists=mysql_num_rows();
if ($itExists)
{
do something
}
mysql_num_rows 是否返回负数整数? PHP 不是总是将 0 等同于 false 吗?我使用了这种方法,没有发现任何不同或不寻常的地方。
I noticed that a lot of tutorial instructions often have this code:
$sql="SELECT * from table";
$num=mysql_num_rows();
if ($num > 0)
{
do something
}
Why do they have to use that condition "if ($num > 0)" when I write this code that I think is compact and readable:
$sql="SELECT * from table";
$itExists=mysql_num_rows();
if ($itExists)
{
do something
}
does mysql_num_rows ever return a negative integer? Doesn't PHP always equate 0 to false? I've used this approach and noticed nothing different or unusual.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
教程可能会尝试使代码尽可能清晰、直观和明确。使用
mysql_num_rows
(其名称显然暗示了一个整数)作为布尔值,有点违反直觉。与零的比较是隐式的。相比之下,如果它被视为一个数字,那么检查它是否大于零是直观且明显的,因此不会那么混乱。Tutorials probably try to make the code as clear, intuitive and explicit as possible. Using
mysql_num_rows
, whose name clearly implies an integer, as a boolean, is somewhat counter-intuitive. The comparison with zero is implicit. By contrast, if it is perceived as a number, then checking that it’s greater than zero is intuitive and obvious and therefore less confusing.为什么要将返回的行数分配给具有
bool
名称(例如itExists
)的变量?通过这样做,您将使以后可以使用的信息变得不那么有用。最好将行数 (mysql_num_rows()) 分配给一个变量,该变量表示它保存许多行(numRows
或在本例中为num
)。将诸如
itExists
之类的内容分配给 4、5 或 6 并不是一个好的做法。正是这样,第一种方法更好。Why would you assign the number of rows returned to a variable with a
bool
name likeitExists
? By doing this you are making information that could be used later less useful. It's better to assign the number of rows (mysql_num_rows()) to a variable that says it holds a number of rows (numRows
or in this casenum
).Having something like
itExists
assigned to 4, 5 or 6 isn't good practice. It's in this way that the first method is better.两种方法都很好用。我不知道你为什么不选择第二种方法。
Both ways work just fine. I don't know why you wouldn't choose the second method.
根据 http://php.net/manual/en/function。 mysql-num-rows.php,出错时返回 FALSE。我很确定
FALSE > 0
为 FALSE,因此两者是等价的。OTOH,在其他语言中(可能在旧版本的 PHP 中),在错误时返回负数是传统的做法。某些 OpenSSL 签名验证函数返回 1 表示“有效”,0表示“无效”,-1 表示“错误”。这是编码风格的问题。
According to http://php.net/manual/en/function.mysql-num-rows.php, it returns FALSE on an error. I'm pretty sure
FALSE > 0
is FALSE, so the two are equivalent.OTOH, it's traditional in other languages (and possibly in older versions of PHP) to return negative numbers on an error. Some OpenSSL signature-verification functions return 1 for "valid", 0 for "invalid", and -1 for "error". It's a coding style thing.
PD。如果只需要检查是否有行,使用 mysql_num_rows 效率不是很高,最好在 SQL 语句内计数:
参见:
- MySQL PHP count(*) 返回一些奇怪的东西
- https://stackoverflow.com/search?q=php+mysql+count+rows
并不总是:
请参阅:http://php.net/manual/en/language .operators.comparison.php
PD. If you only need to check if there are rows, using mysql_num_rows is not very efficient, it is better to count within the SQL statement:
See:
- MySQL PHP count(*) returning something weird
- https://stackoverflow.com/search?q=php+mysql+count+rows
Not always:
See: http://php.net/manual/en/language.operators.comparison.php