检查特定行是否存在mysql

时间:2013-10-11 23:58:10

标签: php jquery mysql mysqli

我有一个mysqli查询,它从表中获取所有图像(我有5个图像显示)。我正在使用jquery滑块来显示它们。问题是,如果没有5个图像我看到空白页面,如果用户只上传两个图像,那么其余三个缩略图将为空,当您点击它们时,它显示空白区域。我不希望发生这种情况,所以如果图像存在而不是显示空缩略图,我如何只显示缩略图?

我尝试了以下但这不起作用。我只需要查看image_one是否存在,然后显示缩略图,其余图像也一样。

<?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 个答案:

答案 0 :(得分:1)

我们无法看到您的数据库设计,因此请查看

的默认值

image_one依此类推

行数始终为01(由于Limit 1) 重要的是字段image_oneimage_five 这些字段始终存在,无论它们是空的还是填充了文件名。

取决于默认值测试

例如

之一
  • if($ row ['image_one']&gt;''){
  • if($ row ['image_one']&gt; null {

放置一个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 ?>

答案 1 :(得分:0)

尝试类似

的内容
   ...
    <ul id="slide"> <!-- This is the thumbnail area -->
        <li class="fbar">&nbsp;</li>
        <?php
        foreach ( $row as $element => $val){
          if(isset($val)) {
            print "<li class='menuItem'><a href=''><img src=".$path.$val."/></a></li>";
          }
        }
        ?>
    </ul>
...

和第一个(<div id="slides">)块

相同

答案 2 :(得分:-1)

我对pdo不是100%肯定,但它不是太远了     

<?php
 $sql = "SELECT * FROM table where title = $title AND id = $id limit 1 ";
 $result = mysql_query($sql);
 while ($row = mysql_fetch_array($result)) {
?>

<div class="slide">
   <a class="fancybox" href="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>" data-fancybox-group="gallery">
      <img class="cloudzoom appsld" src="images/<?php echo $row['DB-IMAGE-PATH-HERE']; ?>"/>
   </a>
</div>

<?php } // end while loop ?>
</div> <!-- end id="slides" -->