我想选择产品详细信息并显示同一列产品的一张图片。
前:
fm_product表
| p_id | p_name | p_price | p_member_id | << (Added product by member)
-----------------------------------------
| 1 | Shirt | 600 | 44 |
| 2 | Pants | 700 | 44 |
| 3 | Shoes | 800 | 45 |
| 4 | Bag | 900 | 45 |
fm_product_image表
| img_id | p_id_img | img_name |
-----------------------------------
| 1 | 1 | Shirt_1.jpg |
| 2 | 1 | Shirt_2.jpg |
| 3 | 1 | Shirt_3.jpg |
| 4 | 2 | Pants_1.jpg |
| 5 | 2 | Pants_2.jpg |
| 6 | 2 | Pants_3.jpg |
| 7 | 3 | Shoes_1.jpg |
| 8 | 3 | Shoes_2.jpg |
| 9 | 4 | Bag_1.jpg |
| 10 | 4 | Bag_2.jpg |
会员ID 44选择添加的产品输出应该如下所示:
| p_name | p_price | img_name |
----------------------------------
| Shirt | 600 | Shirt_1.jpg |
| Pants | 700 | Pants_1.jpg |
会员ID 45选择添加的产品输出应该如下所示:
| p_name | p_price | img_name |
----------------------------------
| Shoes | 800 | Shoes_1.jpg |
| Bag | 900 | Bag_1.jpg |
我的代码
$result= mysql_query("select * from fm_product left join fm_product_image on fm_product_image.p_id_img = fm_product.p_id where p_member_id = '$ID' " ) or die (mysql_error());
while ($row= mysql_fetch_array ($result) ){
$id=$row['p_id'];
<tr>
<td style="text-align:center; margin-top:10px; word-break:break-all; width:450px; line-height:100px;">
<?php if($row['p_id'] != ""): ?>
<img src="<?php echo $row['img_name']; ?>" width="100px" height="100px" style="border:1px solid #333333;">
<?php else: ?>
<img src="images/default.png" width="100px" height="100px" style="border:1px solid #333333;">
<?php endif; ?>
</td>
<td style="text-align:center; word-break:break-all; width:300px;"> <?php echo $row ['p_name']; ?></td>
<td style="text-align:center; word-break:break-all; width:200px;"> <?php echo $row ['p_price']; ?></td>
</tr>
}
答案 0 :(得分:0)
问题解决了。非常感谢你们,但是我已经尝试过这段代码,而且它很有用。
SELECT fm_product.p_name, fm_product_image.img_id, fm_product.p_price, fm_product.p_id, fm_product_image.img_1,GROUP_CONCAT(p_id_img) FROM fm_product LEFT JOIN fm_product_image ON fm_product_image.p_id_img = fm_product.p_id WHERE p_member_id = '8' GROUP BY p_id