用于从数据库返回图像的PHP代码[重复]

This question already has an answer here:

I am fairly new to php and am having some trouble trying to get my images to populate correctly from my database. Any help would be appreciated.

Below is my current code. It is currently returning my default image and not my product image if in the database.

<?php

$query = "SELECT products.giftID, products.gift_name, products.price, products.short_description, product_image.picture FROM products LEFT JOIN product_image ON product_image.giftID='products.giftID' AND products.vendorID='1'";                                 
$result = mysql_query($query) or die ("Database access failed:".mysql_error());

while($row = mysql_fetch_assoc($result)) {
    if($row['picture'] == '') {
        echo "<img src=product_img/image_coming_soon.jpg>";
    } else {
        echo "<img src='".$row['picture'] ."'>";
    }                                       
}
?>  

Formatting the query for readability:

SELECT products.giftID, products.gift_name, products.price,
       products.short_description, 
       product_image.picture
  FROM products
  LEFT JOIN product_image ON product_image.giftID='products.giftID'
                         AND products.vendorID='1'
</div>

Your query joins the product_image table where product_image.giftID='products.giftID'.
I assume you want to match the numeric ID from the products table rather than a literal string.

I suggest removing the quotes from 'products.giftID':

SELECT products.giftID, products.gift_name, products.price, products.short_description, 
       product_image.picture
  FROM products
  LEFT JOIN product_image ON product_image.giftID = products.giftID
                          AND products.vendorID = 1

@showdev has it right. (I made this answer Community Wiki so as not to steal his credit.)

LEFT JOIN product_image   ON product_image.giftID='products.giftID' /*wrong*/
                         AND products.vendorID='1'

is no good, because it looks for a product_image row with a giftID column equal to the text string 'products.giftID'. You want this, without the single quotes in the first line.

LEFT JOIN product_image   ON product_image.giftID=products.giftID
                         AND products.vendorID='1'

Also:

Your HTML code generation is incorrect. You're getting

<img src=product_img/image_coming_soon.jpg>  <!-- wrong -->

The attribute values inside your <img ...> tags have to be surrounded by double quotes. (Some browsers, accidentally, accept other punctuation.) You want stuff like this in your html:

<img src="product_img/image_coming_soon.jpg">

So, you need

{echo '<img src="product_img/image_coming_soon.jpg">';}
else {echo '<img src="' . $row['picture'] . '">';} 

You can put double quotes inside php single quotes.