dougong2005 2016-10-30 08: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 08: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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部