如果没有行返回那么当使用 php 和 mysql 时它是 false

发布于 2024-09-14 23:59:43 字数 392 浏览 1 评论 0原文

我注意到很多教程指令经常有这样的代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不顾 2024-09-21 23:59:43

教程可能会尝试使代码尽可能清晰、直观和明确。使用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.

爱你是孤单的心事 2024-09-21 23:59:43

为什么要将返回的行数分配给具有 bool 名称(例如 itExists)的变量?通过这样做,您将使以后可以使用的信息变得不那么有用。最好将行数 (mysql_num_rows()) 分配给一个变量,该变量表示它保存许多行(numRows 或在本例中为 num)。

将诸如 itExists 之类的内容分配给 4、5 或 6 并不是一个好的做法。正是这样,第一种方法更好。

$pages = $numRows / $itemsPerPage;
//works better than...
$pages = $itExists / $itemsPerPage;

Why would you assign the number of rows returned to a variable with a bool name like itExists? 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 case num).

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.

$pages = $numRows / $itemsPerPage;
//works better than...
$pages = $itExists / $itemsPerPage;
辞旧 2024-09-21 23:59:43

两种方法都很好用。我不知道你为什么不选择第二种方法。

Both ways work just fine. I don't know why you wouldn't choose the second method.

孤独难免 2024-09-21 23:59:43

根据 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.

风情万种。 2024-09-21 23:59:43

PD。如果只需要检查是否有行,使用 mysql_num_rows 效率不是很高,最好在 SQL 语句内计数:

SELECT COUNT(*) FROM table

参见:
- MySQL PHP count(*) 返回一些奇怪的东西
- https://stackoverflow.com/search?q=php+mysql+count+rows


PHP 不是总是将 0 等同于 false 吗?

并不总是:

$a = 0;
if (!$a) echo 'pass <br />'; else echo 'fails <br />';
if ($a == false) echo 'pass <br />'; else echo 'fails <br />';
if ($a === false) echo 'pass <br />'; else echo 'fails <br />';

请参阅: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:

SELECT COUNT(*) FROM table

See:
- MySQL PHP count(*) returning something weird
- https://stackoverflow.com/search?q=php+mysql+count+rows


Doesn't PHP always equate 0 to false?

Not always:

$a = 0;
if (!$a) echo 'pass <br />'; else echo 'fails <br />';
if ($a == false) echo 'pass <br />'; else echo 'fails <br />';
if ($a === false) echo 'pass <br />'; else echo 'fails <br />';

See: http://php.net/manual/en/language.operators.comparison.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文