简体   繁体   中英

MYSQL row count is NULL but I need to display 0

I have a simple row count in PHP. When no rows are found, I need $sfideRicevute to be '0', but this query returns me nothing.

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.

1, true and other numbers will keep their value however, I don't know if that's clear right away :)

Please review http://php.net/manual/en/language.types.type-juggling.php for more information.

From PHP docs what mysql_num_rows returns:

The number of rows in a result set on success or FALSE on failure.

It should be returning 0, if it isn't, there's likely something wrong with the query.

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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