dongping1689 2015-09-27 09:48
浏览 31
已采纳

当mysql表没有数据时,获取消息而不是表

I'm quite new at PHP and have created a simple table that gets the data from a mysql database and displays it. However I want that, when the 'region' field has no records, to get a message 'No records' instead of the table. On the other hand, if the 'region' field has data, then the table should appear normally.

So, in the example below, if there are no records for the region'Alsace', no table should appear...

It seems quite simple in principle, but I cannot seem to find a way of doing this... .

Here is my code:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = "tournois";


$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Could not connect : " . mysql_error());



mysql_select_db($db, $conn)
or die("Could not select database");

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

$query = "SELECT date, tournoi, lieu, region, prix,ritme, link FROM tournois_france WHERE region = 'Alsace'";
$result = mysql_query($query);

if ($result){?>
<table class='table table-hover'>
<tr class=info>
<th>Date</th>
<th>Tournois</th>
<th>Lieu</th>
<th>Region</th>
<th>Prix</th>
<th>Ritme</th>
</tr>

<?php while($row = mysql_fetch_array($result)) {?>
<tr>
<td><?php echo $row->Date;?></td>
<td><?php echo $row->Tournoi;?></td>
<td><?php echo $row->Lieu;?></td>
<td><?php echo $row->Region;?></td>
<td><?php echo $row->Prix;?></td>
<td><?php echo $row->Ritme;?></td>
</tr>
<?php }?>        
</table>
<?php } ?>   
  • 写回答

2条回答 默认 最新

  • doujiang1939 2015-09-27 09:59
    关注

    Try the below,

    Use mysql_num_rows this will check the return rows count

    <?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $db = "tournois";
    
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die("Could not connect : " . mysql_error());
    
    
    
    mysql_select_db($db, $conn)
    or die("Could not select database");
    
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET character_set_connection=utf8');
    mysql_query('SET character_set_client=utf8');
    mysql_query('SET character_set_results=utf8');
    
    $query = "SELECT date, tournoi, lieu, region, prix,ritme, link FROM tournois_france WHERE region = 'Alsace'";
    $result = mysql_query($query);
    
    if (mysql_num_rows($result) !=0){?>
    <table class='table table-hover'>
    <tr class=info>
    <th>Date</th>
    <th>Tournois</th>
    <th>Lieu</th>
    <th>Region</th>
    <th>Prix</th>
    <th>Ritme</th>
    </tr>
    
    <?php while($row = mysql_fetch_array($result)) {?>
    <tr>
    <td><?php echo $row->Date;?></td>
    <td><?php echo $row->Tournoi;?></td>
    <td><?php echo $row->Lieu;?></td>
    <td><?php echo $row->Region;?></td>
    <td><?php echo $row->Prix;?></td>
    <td><?php echo $row->Ritme;?></td>
    </tr>
    <?php }?>        
    </table>
    <?php }else{ ?>
    <h4>No data found </h4>
    <?php } ?>   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来