没有结果回来的消息

时间:2016-05-29 13:09:43

标签: php

如果没有返回任何结果,我想显示“此活动没有门票”,否则显示门票数据。

代码有问题吗?

                <?php 

        $result = mysqli_query($con,"SELECT * FROM ticket WHERE eventID = ".$_GET['eventID'] ." ORDER by ticketEDate ASC");
        while($row = mysqli_fetch_array($result, MYSQL_BOTH))  {

                                if (mysqli_num_rows($result) ==0){
                                        echo "There are no tickets for this event";
                                    } else {

                        echo '<tr>
                                <td><h8>Ticket voor: </h8><h8a>'. $row['ticketName'] .'</h8a></td><br>

                                <td><h4><strong>Begin verkoop:</strong> &nbsp; ',date("d.m.Y", strtotime ($row['ticketSDate'])),' &nbsp; ', date('H:i', strtotime ($row['ticketSTime'])).' hr.<h4></td>
                                <td><h4><strong>Einde verkoop:</strong> &nbsp; ',date("d.m.Y", strtotime ($row['ticketEDate'])),' &nbsp; ', date('H:i', strtotime ($row['ticketETime'])).' hr.<h4></td>
                                <td><h5><strong>Beschrijving:</strong></color> &nbsp;'. $row['ticketDescription'] .'</h5></td>
                                <td><h4><strong>Event ID:</strong> &nbsp;'. $row['eventID'] .' '. $row['eventName'] .'</h4></td>        

                                <form action="registratie.php" method="get">
                                <td><h4>'. $row['ticketID'] .'  <input name="book" type="submit" value="Kies dit ticket" />

                                <input type="hidden" name="ticketID" value="'.$row['ticketID'].'"> 
                                </form>

                                <br><br>
                            </tr>';
                    }
                }
            ?> 

2 个答案:

答案 0 :(得分:2)

if (mysqli_num_rows($result) ==0){移到while之外 - 循环

if (mysqli_num_rows($result) == 0){
    echo "There are no tickets for this event";
} else {
     while ($row = mysqli_fetch_array($result, MYSQL_BOTH)) {
         // render rows
     }
}

答案 1 :(得分:0)

由于int 0 被视为false,我们可以使用:

if(!empty(mysqli_num_rows($result))){
    while($row = mysqli_fetch_array($result)) {
     //...
    }
}else{
    echo "There are no tickets for this event";
}