检查特定行是否存在 mySQL


check if specific row exists mysql

我有一个 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>

我们看不到您的数据库设计,因此请查看您的默认值

image_one等等

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

根据默认值测试它

例如以下之一

  • if ($row['image_one']> ''( {
  • if ($row['image_one']>空 {

放一个if围绕建筑 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 ?>

尝试类似

   ...
    <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">(块也是如此

我不是 100% 确定 pdo,但它不太远

<?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" -->