简体   繁体   English

MYSQL行数为NULL,但我需要显示0

[英]MYSQL row count is NULL but I need to display 0

I have a simple row count in PHP. 我在PHP中有一个简单的行数。 When no rows are found, I need $sfideRicevute to be '0', but this query returns me nothing. 当找不到行时,我需要$ sfideRicevute为'0',但是此查询没有返回任何内容。

PHP: PHP:

<?php
    $sql = "SELECT * FROM `match` WHERE show_id = $showid AND w02 = $id AND status = 'sfida'";

    if ($result = mysql_query ("$sql")){
        while ($row=mysql_fetch_array ($result)) {

?>
<? $sfideRicevute = mysql_num_rows($result); ?>
<? echo $sfideRicevute; ?>

Thanks for helping 感谢您的帮助

echo intval($sfideRicevute);

or 要么

echo (int) $sfideRicevute

Both cast the value to integer 0. In php '0', false, 0, null will all be converted to int 0 this way. 两者都将值转换为整数0。在php'0'中,false,0,null将全部以这种方式转换为int 0。

1, true and other numbers will keep their value however, I don't know if that's clear right away :) 1,true和其他数字将保持其值,但是我不知道是否马上就可以知道:)

Please review http://php.net/manual/en/language.types.type-juggling.php for more information. 请查看http://php.net/manual/en/language.types.type-juggling.php了解更多信息。

From PHP docs what mysql_num_rows returns: 从PHP文档中,mysql_num_rows返回什么:

The number of rows in a result set on success or FALSE on failure. 成功时结果集中的行数,失败时结果为FALSE。

It should be returning 0, if it isn't, there's likely something wrong with the query. 它应该返回0,如果不是,则查询可能有问题。

Also note that using these mysql_ functions are not recommended, since they are going to be removed from php, see http://nl3.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated 另请注意,不建议使用这些mysql_函数,因为它们将从php中删除,请参见http://nl3.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated

Second: have you thought about SQL injections, as you are putting a variable into the query directly, which also isn't recommended and not necessary with the alternatives mentioned in the link above. 第二:您是否考虑过SQL注入,因为您将变量直接放入查询中,因此也不建议使用,也不需要使用上面链接中提到的替代方法。

您可以查看$ sfideRicevute的值是什么,如果为空,则显示0。

echo $sfideRicevute == null ? 0 : $sfideRicevute;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM