如果表为空,请告诉我没有条目

时间:2014-12-02 22:10:11

标签: php if-statement echo

我试图显示一个表格,当它表示空白时表示"没有符号Ups"

我正确地显示了表格,但是当我尝试运行会回显的内容时,我总是会收到一条内部错误消息"没有Sign Ups"当表是空的时候。这是我在尝试回应&#34之前使用的原始代码;没有Sign Ups"

<div><div align="center">
<?php
$con=mysqli_connect("Server","UN","PW","DBNAME");
// Check connection
if (mysqli_connect_errno())

{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/* 
SELECT * FROM table_name ORDER BY id_location 
 */ 

$result = mysqli_query($con,"SELECT * FROM `table_name` ORDER BY signupdate ASC , id_location ASC ");

echo "<div class='section group'>
            <div class='col span_5_of_5'>
            Signed Up So Far
            </div>

      </div>
        ";

while($row = mysqli_fetch_array($result))
{
            echo "<div class='section group'>";
            echo "<div class='col span_1_of_5' style='background-color:grey; color:white; font-size:12px;'>" . $row['id'] . "</div>";
            echo "<div class='col span_1_of_5'>" . $row['name'] ." & ". $row['partner'] . "</div>";
            echo "<div class='col span_1_of_5'>" . $row['id_location'] . "</div>";
            echo "<div class='col span_1_of_5'>" . $row['from_time'];
            echo " to " . $row['to_time'] . "</div>";
            $date = new DateTime($row['signupdate']);

            echo "<div class='col span_1_of_5' style='color:grey; font-size:12px;'>" . $date->format('l M d, Y');



            echo "</div>";
}
echo "</div>";

mysqli_close($con);
?>
</div>

1 个答案:

答案 0 :(得分:0)

试试这段代码

 $result = mysqli_query($con,"SELECT * FROM `table_name` ORDER BY signupdate ASC , id_location ASC ");

$row_cnt = $result->num_rows;

if ($row_cnt == 0 ){
    echo "No results";
} else {
echo "<div class='section group'>
            <div class='col span_5_of_5'>
            Signed Up So Far
            </div>

      </div>
        ";

while($row = mysqli_fetch_array($result))
{
            echo "<div class='section group'>";
            echo "<div class='col span_1_of_5' style='background-color:grey; color:white; font-size:12px;'>" . $row['id'] . "</div>";
            echo "<div class='col span_1_of_5'>" . $row['name'] ." & ". $row['partner'] . "</div>";
            echo "<div class='col span_1_of_5'>" . $row['id_location'] . "</div>";
            echo "<div class='col span_1_of_5'>" . $row['from_time'];
            echo " to " . $row['to_time'] . "</div>";
            $date = new DateTime($row['signupdate']);

            echo "<div class='col span_1_of_5' style='color:grey; font-size:12px;'>" . $date->format('l M d, Y');



            echo "</div>";
}
echo "</div>";

}