我使用简单的表单和一些php将一些图像上传到MySQL数据库。 该表格具有以下选择:c / p>。
<select name="image_ctgy">
<option value="animals">animals</option>
<option value="vegetables">vegetables</option>
<option value="minerals">minerals</option>
</select>
每个图像在MySQL表'image_ctgy'中都有其中一个类别。 现在我想将动物类别中的图像回显到我的网页。
我使用以下代码获取我网页上的所有图片:
<?php
/*** Check the $_GET variable ***/
try {
/*** connect to the database ***/
$dbh = new PDO("mysql:host=;dbname=", '', '');
/*** set the PDO error mode to exception ***/
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** The sql statement ***/
$sql = "SELECT image_id, thumb_height, thumb_width, image_ctgy, image_type, image_name, title FROM testblob";
/*** prepare the sql ***/
$stmt = $dbh->prepare($sql);
/*** exceute the query ***/
$stmt->execute();
/*** set the fetch mode to associative array ***/
$stmt->setFetchMode(PDO::FETCH_ASSOC);
/*** set the header for the image ***/
foreach($stmt->fetchAll() as $array)
{
echo '<div class="thumb" style="width: '.$array['thumb_width'].'px; height: '.$array['thumb_height'].'px;">
<p class="title">' . $array["title"] . '</p>
<p><a href="showfile.php?image_id='.$array['image_id'].'">
<img src="showthumbs.php?image_id='.$array['image_id'].'" alt="'.$array['image_name'].' /">
</a></p>
</div>';
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
我想我需要在foreach循环中使用if语句来检查图像是否必须合并动物。谁能帮我这个?感谢
答案 0 :(得分:0)
Raveenanigam在我的问题上发布了一个问题。
您可以使用WHERE子句进行查询更改:WHERE category =&#39; Animal&#39;