[英]check if specific row exists mysql
I have a mysqli query which fetches all the images from the table(I have 5 images that I display). 我有一个mysqli查询,可从表中获取所有图像(我有5个图像显示)。 I am using a jquery slider to display them. 我正在使用jquery滑块显示它们。 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. 问题是如果没有5张图像,我会看到空白页,例如,如果用户仅上传两张图像,则其余三个缩略图将是空白的,当您单击它们时,它将显示空白区域。 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. 我只需要查看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"> </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>
we can not see your database design so look at your default value for 我们看不到您的数据库设计,因此请查看您的默认值
image_one
and so on image_one
等
The row count is allways 0
or 1
(because of Limit 1
) 行计数始终为0
或1
(由于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. 重要的是image_one
到image_five
字段这些字段始终存在,无论它们为空还是使用文件名填充。
depending on the default value test it 根据默认值测试一下
for example one of 例如以下之一
put an if
arround building html. 放一个if
arround构建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 ?>
try something like 尝试类似
...
<ul id="slide"> <!-- This is the thumbnail area -->
<li class="fbar"> </li>
<?php
foreach ( $row as $element => $val){
if(isset($val)) {
print "<li class='menuItem'><a href=''><img src=".$path.$val."/></a></li>";
}
}
?>
</ul>
...
and the same for the 1st ( <div id="slides">
) block 与第一个( <div id="slides">
)块相同
Im not 100% sure for pdo but its not too far away 我不能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" -->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.