当数据库查询没有返回结果时,如何显示消息?

时间:2015-04-11 11:21:19

标签: php while-loop

我有这段代码:

<?php

$platform_id = 7;
$query="SELECT * FROM game WHERE game_id IN (SELECT game_id FROM game_platform WHERE platform_id=".$platform_id.") AND game_release BETWEEN '2015-08-01' AND '2015-08-31' ORDER BY game_release";

$result=mysqli_query($link,$query) or die (mysqli_error());


        echo"<table border=1>
        <tr>
            <th width=40px>Day</th>
            <th width=270px>Title</th>
            <th width=203px>Genre</th>
            <th width=203px>Developer</th>
            <th width=205px>Publisher</th>
            <th width=61px>Retail?</th>
            <th width=30px height=30px></th>
            <th width=104px>Note</th>
        </tr>";

        while($row=mysqli_fetch_assoc($result))
       {
        echo"
        <tr>
            <td> ". (new DateTime($row['game_release']))->format("j") ." </td>
            <td>{$row['game_name']}</td>

            <td>";
            $query="SELECT * FROM genre WHERE genre_id=".$row['game_genre']; 
            $genreresult=mysqli_query($link,$query);                                    $genrerow=mysqli_fetch_assoc($genreresult);
        echo $genrerow['genre_name'];
        echo "</td>
            <td>{$row['game_dev']}</td>
            <td>{$row['game_pub']}</td>
            <td>{$row['game_type']}</td>
            <td><a href=\"{$row['game_site']}\" target=\"_blank\"><img src=\"images/officialwebsite.png\" title=\"Official website\"/></a>
                <a href=\"{$row['game_trailer']}\" target=\"_blank\"><img src=\"images/youtube.png\" title=\"Trailer\"/></a></td>
            <td>{$row['game_note']}</td>
        </tr>";


       }

       echo"</table>";
?>  

我希望在上个月没有发布时显示一条消息,因为现在我得到的是一张空白的桌子,它不是真正的用户友好型,也没有视觉吸引力。

我在此网站上找到了此代码,但由于.isEmpty而导致语法错误。

<?php

$platform_id = 7;
$query="SELECT * FROM game WHERE game_id IN (SELECT game_id FROM game_platform WHERE platform_id=".$platform_id.") AND game_release BETWEEN '2015-08-01' AND '2015-08-31' ORDER BY game_release";

$result=mysqli_query($link,$query) or die (mysqli_error());


        echo"<table border=1>
        <tr>
            <th width=40px>Day</th>
            <th width=270px>Title</th>
            <th width=203px>Genre</th>
            <th width=203px>Developer</th>
            <th width=205px>Publisher</th>
            <th width=61px>Retail?</th>
            <th width=30px height=30px></th>
            <th width=104px>Note</th>
        </tr>";

        if($result.isEmpty())
        echo "Unfortunately there are no releases this month!";

        else
        {

        while($row=mysqli_fetch_assoc($result))
        {
        echo"
        <tr>
            <td> ". (new DateTime($row['game_release']))->format("j") ." </td>
            <td>{$row['game_name']}</td>

            <td>";
            $query="SELECT * FROM genre WHERE genre_id=".$row['game_genre']; 
            $genreresult=mysqli_query($link,$query);                                    $genrerow=mysqli_fetch_assoc($genreresult);
        echo $genrerow['genre_name'];
        echo "</td>
            <td>{$row['game_dev']}</td>
            <td>{$row['game_pub']}</td>
            <td>{$row['game_type']}</td>
            <td><a href=\"{$row['game_site']}\" target=\"_blank\"><img src=\"images/officialwebsite.png\" title=\"Official website\"/></a>
                <a href=\"{$row['game_trailer']}\" target=\"_blank\"><img src=\"images/youtube.png\" title=\"Trailer\"/></a></td>
            <td>{$row['game_note']}</td>
        </tr>";


       }
       }

       echo"</table>";
       ?>

我一直在阅读,似乎不是另一种方式,因为while-loops没有&#39; else

我该如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:4)

你好像混合了javascript和php,因为.isEmpty()不是php函数。

在显示表格之前,您需要检查row count,例如:

$result=mysqli_query($link,$query) or die (mysqli_error());
if (mysqli_num_rows($result) > 0) {
   // your table generation code
} else {
  echo "Unfortunately there are no releases this month!";
}