dougong2005 2016-10-30 16:14
浏览 29
已采纳

从最多3个不同的表中请求PDO中的数据

What am I trying to do?

I'm trying to display Invoices data on the Dashboard page of my web application. The tiny road block that I have came upon is displaying data based on integers. For example;

Invoice 1 -> Bill User 1

What I'm trying to achieve is having that integer for either a User or Company to whom the Invoice is assigned display their name, example;

BillContact->1 (becomes) BillContact->John Smith 

Here is the current output of my data in PHP from PDO enter image description here

If I was using old school MySQL I would write secondary query but I know this isn't much needed in PDO as you can just use UNION or JOIN, problem is I'm not very skilled with PDO or UNION/JOIN function, and would like to use this question as a way to learn how they work and use them in my code for future database queries.

As you can see Contact Billed are there assigned values, which I wish to replace with the Contact's name from Contacts table in the same Database.

Here is my PDO code to pull the current data.

$database->query('SELECT * FROM invoices LIMIT 10');
$rows = $database->resultset();

echo ... table html here

foreach($rows as $row)
                    {
                    ... td html here
                    }

echo ...

And here is the values I wish to pull from Contacts/Companies tables depending on id being passed.

contacts           companies
.firstname         .companyname       
.lastname

enter image description here

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dounuo9921 2016-10-30 16:28
    关注

    Here's a simple join to solve your issue.

    try
    {
    $s = $conn->prepare("SELECT inv.*,c.* from invoices inv, contacts c where inv.contact_billed = c.id");
    $s->execute();
    }
    catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大