dporb84480 2013-10-11 23:58
浏览 21
已采纳

检查特定行是否存在mysql

I have a mysqli query which fetches all the images from the table(I have 5 images that I display). I am using a jquery slider to display them. The problem is if there are no 5 images I see blank page like if the user uploads just two images then the rest three thumbnails will be empty and when you click on them it shows empty area. I don't want that happen so how do I only show the thumbnail if the image exists instead of showing empty thumbnail?

I tried the below but this doesn't work. I just need to see if image_one exists then show the thumbnail and the same for the rest of the images.

<?php
 $stmt = $mydb->prepare("SELECT * FROM table where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
 $result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$path = 'images/';
?>


<div id="slides">

<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_one']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_one']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_two']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_two']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?> <div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_three']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_three']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_four']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_four']?>"/></a></div><?php };?>
<?php if($result->num_rows > 0){?><div class="slide"><a class="fancybox" href="<?php echo $path.$row['image_five']?>" data-fancybox-group="gallery"><img class="cloudzoom appsld" src="<?php echo $path.$row['image_five']?>"/></a></div><?php };?>

</div>

<div id="slide_menu">

<ul id="slide"> <!-- This is the thumbnail area -->
    <li class="fbar">&nbsp;</li>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_one']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_two']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_three']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_four']?>"  /></a></li><?php }; ?>
    <?php if($result->num_rows > 0){?><li class="menuItem"><a href=""><img src="<?php echo $path.$row['image_five']?>"  /></a></li><?php }; ?>
</ul>


</div>
  • 写回答

3条回答 默认 最新

  • dongliuliu0385 2013-10-12 02:05
    关注

    we can not see your database design so look at your default value for

    image_one and so on

    The row count is allways 0 or 1 (because of Limit 1)
    Important are the fields image_one to image_five These fields are always present, regardless of whether they are empty or with file names are filled.

    depending on the default value test it

    for example one of

    • if ($row['image_one'] > '') {
    • if ($row['image_one'] > null {

    put an if arround building html.

    <?php if($result->num_rows > 0){?>
     <?php if ($row['image_one'] > '') 
       {?>
         <li class="menuItem">
         <a href=""><img src="<?php echo $path.$row['image_one']?>" /></a>
         </li>
       <?php 
       }?>
        .... next 4 other tests
     <?php if ($row['image_two'] > '') 
        ....
    
    <?php } // END__$result->num_rows > 0 ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行