dongyingming8970
2014-11-04 02:29 阅读 59
已采纳

如何通过MySQL在PHP中显示特定图像

I'm trying to display each specific image to display in PHP/HTML page, it's currently displaying all the pages but I would like it to only display one for whenever I type in the function.

My code:

 <div class="ser-grid-list img_style">
                            <div class="gallery1">


                            </div>
                        </div>
                        <div class="ser-grid-list img_style">
                            <div class="gallery1">

                            </div>
                        </div>
                        <div class="ser-grid-list img_style">
                            <div class="gallery1">

                            </div>
                        </div>
                        <div class="ser-grid-list img_style">
                            <div class="gallery1">
                                                            <?php
 $sql = "SELECT image FROM product"; //sql query
 $result = mysql_query($sql) or die(mysql_error($connection));
 while ($row = mysql_fetch_array($result)) //display the results
 {
 echo "<p><img src='../images/" . ($row['image']) . "'" . " " . " />"; 
 }

?>

What its doing:

http://puu.sh/cCs29/6769667d43.jpg

What I'm wanting to make them do is align from left to right. It was working with Static images coming through html but I decided to use a database. This was it prior

http://puu.sh/cCs5g/919c0c8c57.jpg

Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douzhicui2209 douzhicui2209 2014-11-04 02:30

    Simply get the id of your image just like this one

     $sql = "SELECT image FROM product where id=101";
    

    This is the easiest one? Am i missing something?

    点赞 评论 复制链接分享
  • doulai7239 doulai7239 2014-11-04 02:39

    <p> is a block element, so it'll start and end with a new line. If you want to display them inline, try eliminating the <p> tags surrounding the <img> tags.

    And as @CodeSlayer suggests, if you only want one single image returned, add a WHERE clause to your query to fetch a specific image.

    点赞 评论 复制链接分享

相关推荐