繁体   English   中英

php echo如果查询为空

[英]php echo if query is empty

我正在查询数据库,但是当结果为空时,我想输出显示“无内容”的表行,但是if似乎总是返回true。

这是我的代码...

$priorityincidentsQ         = mysql_query("SELECT * FROM applications WHERE pi >= ('2') ");
while($priorityincidentsR   = mysql_fetch_object($priorityincidentsQ)) 
  {
    if (empty($priorityincidentsR)) {
      echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";    
    } else {        
      echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
      echo "<td  class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>"; 
    }
  }

使用mysqli_num_rows()检查是否有任何结果:

    $conn = mysqli_connect($host, $user, $password, $database);
    $priorityincidentsQ = mysqli_query($conn, "SELECT * FROM applications WHERE pi >= ('2') ");
    if (mysqli_num_rows($priorityincidentsQ) > 0){
        while ($priorityincidentsR = mysqli_fetch_object($priorityincidentsQ)) {
            echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
            echo "<td  class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
        }
    }else{    
        echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";
    }

是的,最好使用mysqli_*函数而不是mysql_*

仍然从未弄清楚为什么这种方式对我不起作用,我直接在SQL工作台中尝试了查询,一切看起来都应该如此,最终解决了这样的问题。

<!-- priority incidents-->
<?php

$priorityincidentsQ         = mysql_query("SELECT * FROM applications WHERE pi >= ('1') ");
while($priorityincidentsR   = mysql_fetch_object($priorityincidentsQ)) 
    {
    echo "<tr><td class=\"closedcallscell\"><b><a href=\"".DIR."?p=$priorityincidentsR->pageID\">$priorityincidentsR->application_friendly_name</a></b></td>";
    echo "<td  class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
    }

?>

<!-- if no incidents-->
<?php

$incidentNumberofRowsQ      = mysql_query("SELECT COUNT(*)numberofrows FROM applications WHERE pi >= ('1') ");
while($incidentNumberofRowsR    = mysql_fetch_object($incidentNumberofRowsQ)) 
    {

        if ($incidentNumberofRowsR->numberofrows == '0')
        {
                echo "<tr><td class=\"closedcallscell centered\"><b>Currently no priority incidents</b></td>";
        }
    }

?>

看起来似乎很愚蠢,但是至少可以起作用。 谢谢大家的帮助。 :)

暂无
暂无

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

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