dsa80833
2015-08-10 11:54
浏览 52
已采纳

从MySQL获取特定数据并使用PHP回显它

I am new here and just registered to ask a question. First of all i am newbie to PHP,so i have not tons of experience in it. My problem is the following:

I got two tables in my database. The first one is called products. The products table gives new products in my shop an unique ID. The second one is the products_to_categories table, in this table the products_id gets an cateogries_id so you know which product belongs to which category.

Now i need to echo the images (products_images in products-table) from the last 4 products (there is also a products_date_added attribute) of a category. All i managed to do until now is to echo the links of the images from the product tables:

mysql_select_db('example2');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
    echo "Product Image : {$row['products_image']}  <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully
";
mysql_close($conn);

?>    
</body>

could someone help me to do that? thanks in advance!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzai3917 2015-08-10 12:00
    已采纳

    Wrap img tags (html) around the links you echo, so

    echo "Product Image : {$row['products_image']}  <br> ".
    

    becomes

    echo "Product Image : <img src=\"{$row['products_image']}\"/>  <br> ".
    

    and as for the results, you can limit the search by using LIMIT 4 and ORDER BY to sort it by the date products_date_added

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题