我有一个 PHP 代码可以从具有相同列“ProductID”的 2 个不同表“tbl_products”和“tbl_productphotos”中获取数据,但我无法从“tbl_productphotos”中获取图像。
tables data is: 'tbl_products'
ProductID Title Description tilename
36 T-shirt T-Shirt Red Glass Tile
37 Pant Pant Black Glass Tile
'tbl_productphotos'
id ProductID photo
1 36 image1.jpeg
2 36 image2.jpeg
3 37 imagepant.jpeg
我的查询是:
<?php
$sql="select tbl_products.*,tbl_productphotos.* from tbl_products inner join tbl_productphotos on tbl_products.ProductID=tbl_productphotos.ProductID where tbl_products.tilename='Glass Tile' ";
$qex=mysql_query($sql);
while($row=mysql_fetch_array($qex))
{
?>
& 在这里打印:
<input type="hidden" value="<?php echo $row['ProductID'];?>">
<li class="col-md-3 col-sm-6 col-xs-12 isotope-item websites" style="float: left">
<div class="portfolio-item">
<span class="thumb-info thumb-info-lighten thumb-info-bottom-info thumb-info-centered-icons">
<span class="thumb-info-wrapper">
<img src="images/products/big/<?php echo $row['photo'];?>" class="img-responsive" alt="" height="200px" width="200px">
<span class="thumb-info-title">
<span class="thumb-info-inner"><?php echo $row['Title'];?></span>
<span class="thumb-info-type"><?php echo substr($row['Description'],0 ,37);?></span>
</span>
<span class="thumb-info-action">
<a href="portfolio-single-project.html">
<span class="thumb-info-action-icon thumb-info-action-icon-primary"><i class="fa fa-link"></i></span>
</a>
<a href="img/projects/project.jpg" class="lightbox-portfolio">
<span class="thumb-info-action-icon thumb-info-action-icon-light"><i class="fa fa-search-plus"></i></span>
</a>
</span>
</span>
</span>
</div>
</li>
<?php
}
?>
最佳答案
您可以为相同的列名定义别名:products_ProductID
和 productphotos_ProductID
。
$sql = "select tbl_products.*,tbl_productphotos.*, tbl_products.ProductID as products_ProductID, tbl_productphotos.ProductID as productphotos_ProductID from tbl_products inner join tbl_productphotos on tbl_products.ProductID=tbl_productphotos.ProductID where tbl_products.tilename='Glass Tile' ";
关于php - 如何获取不同表的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968140/