douxi3404 2018-06-13 21:09
浏览 47
已采纳

关系MySQL PHP

I have a table named "prices" in which I have two fields where I reference two other tables named "prodcuts" and "supliers". In the "prices" table I do reference to the "id" of the "prodcuts" and "supliers" and I need to get the name of both in a php code... What I have so far is this, but I don't know how to make it work:

$result = mysqli_query($conn, "SELECT * FROM prices") or die("Could not find");

  if (mysqli_num_rows($result) > 0) {
     while ($rr = mysqli_fetch_assoc($result)) { 
         $a = $rr['id'];
         $b = $rr['date'];
         $c = $rr['product'];
         $d = $rr['suplier'];
         $e = $rr['quantity'];
         $f = $rr['packaging'];
         $g = $rr['event'];
         $h = $rr['price'];

         $prov .="
               <tr>                                  
                 <td>".$b."</td>
                 <td><a href='productos-result.php?search=".$c."'>".$c."</a></td>
                 <td><a href='proveedores-result.php?search=".$d."'>".$d."</a></td>
                 <td>".$e."</td>
                 <td>".$g."</td>
                 <td>$".$h."</td>    
               </tr>";
        }
} 

I need to display the name of the product $c and suplier $d instead of the id, my tables are like this:

prices

id, date, productid, suplierid, quantity, event, price

product

id, name, description

suplier

id, name, description

  • 写回答

1条回答 默认 最新

  • dongshen6060 2018-06-13 21:32
    关注

    Change your query to join the products and suppliers and have their names in the list of columns aliased to what you want.

    SELECT prices.id,
           prices.date,
           product.name product,
           suplier.name suplier,
           prices.quantity,
           prices.packaging,
           prices.event,
           prices.price
           FROM prices
                INNER JOIN product
                           ON product.id = prices.productid
                INNER JOIN suplier
                           ON suplier.id = prices.suplierid;
    

    (Change the INNER JOIN to LEFT JOIN if you have prices without supplier or product but want to show them anyway (with empty names).)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line