php - 如何获取不同表的数据?

标签 php jquery html css

我有一个 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_ProductIDproductphotos_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/

相关文章:

javascript - 在 x-editable 中发送自定义参数

javascript - 如何仅使用 CSS 在 Select 显示 'Other' 时显示表单上的文本字段

javascript - 为什么 JavaScript 添加的按钮没有默认间距?

php - 如何生成动态加载数据库的所有可能结果?

php - 为什么多词 Doctrine 搜索会导致 MySQL 使用高达 300% CPU?

jquery - 使用 "columns.data"和 JSON 数据右对齐 JQuery 数据表单元格?

html - 设置嵌套的 html 元素可见

javascript - 通过 php 上传 Angular JS 文件——未定义索引 :

php - 在浏览器 session 中存储表单信息的替代方法

jquery - Internet Explorer (IE) 的 CSS/HTML 兼容性问题