dsqa6272 2014-08-09 18:16 采纳率: 0%
浏览 56
已采纳

在PHP中使用嵌套查询生成JSON对象

I'm trying to pass JSON Object from PHP like this:

[{
    "id":"user1",
    "shops": [{
        "id":"shop1",
        "invoices": [{
            "id":"invoice1",
            "details": [{
                "id":"detailA"
            }, {
                "id":"detailB"
            }]
        }, {
        "id":"shop2",
        "invoices": [{
            "id":"invoice2",
            "details": [{
                "id":"detailC"
            }, {
                "id":"detailD"
            }]
        }]
    }]
}]

I have this function and it's working, but when database returns a lot of records I get memory limit exhausted. I don't want to increase memory limit. What is the best practice?

function loadusers() {
    $return = array();

    $query = "SELECT u.id FROM users u";
    $users = mysql_query($query);

    while($user = mysql_fetch_assoc($users)) {
        $query = "SELECT s.id FROM shops s WHERE s.id_user = ".$user['id'];
        $shops = mysql_query($query);

        while($shop = mysql_fetch_assoc($shops)) {                
            $query = "SELECT i.id FROM invoices i WHERE i.id_shop = ".$shop['id'];
            $invoices = mysql_query($query);

            while($invoice = mysql_fetch_assoc($invoices)) {                
                $query = "SELECT d.id FROM invoice_details d WHERE d.id_inv = ".$invoice['id'];
                $details = mysql_query($query);

                while($detail = mysql_fetch_assoc($details)) {                
                    $invoices['details'][] = $detail;
                }

                $shop['invoices'][] = $invoice;
            }

            $user['shops'][] = $shop;
        }

        $return[] = $user;
    }

    return $return;
}

Thanks in advance.

Following fiction's answer:

I created new query:

SELECT
    u.id as id_user, 
    s.id as id_shop,
    i.id as id_invoice,
    d.id as id_detail
FROM 
    users u LEFT JOIN shops s ON s.id_user = u.id
    LEFT JOIN invoices i ON i.id_shop = s.id
    LEFT JOIN invoice_details d ON d.id_invoice = i.id

Result:

Query result

Is there a syntax sugar to convert it to expected JSON object like above or do I have to manually parsing the result into PHP array then do the json_encode?

  • 写回答

1条回答 默认 最新

  • douyan2970 2014-08-09 18:29
    关注

    You can do it with one query using JOINS and you will not be needed cycles, query for your situation:

    SELECT 
        users.id as uid, 
        shops.id as sid,
        invoices.id as iid,
        invoice_details.id as did
    FROM 
        users INNER JOIN shops ON shops.id_user = users.id,
        invoices INNER JOIN shops ON invoices.id_shop = shops.is,
        invoice_details INNER JOIN invoices ON invoice_details.id_inv = invoices.id;
    

    Maybe it will be better for you to use another join (not inner), depending on what you want, so some more about joins: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动