简体   繁体   English

将MySQL查询作为多维数组返回

[英]Return MySQL query as multidimensional array

I have an SQL query, where i do some joins, but when i var_dump the result in PHP i get an array, with a lot of duplicates. 我有一个SQL查询,我做了一些连接,但是当我在PHP中var_dump结果时,我得到一个数组,有很多重复。 Is there any way to get the result back as an multidimensional array like: 有没有办法将结果作为多维数组返回,如:

array( array("projectname", "projectdescription", "projectthumb" , "activestate"),
           array("img1.jpg", "img2.jpg", "img3.jpg"),
           array("tech1", "tech2", "tech3") 
         ); 

At the moment i get the following result: http://pastebin.com/ZF7yiafS 目前我得到以下结果: http//pastebin.com/ZF7yiafS

The SQL query: SQL查询:

SELECT    p.name,
                              p.description,
                              p.img_thumb,
                              p.active,
                              t.name,
                              i.filename
                    FROM      projects as p
                    JOIN      projects_x_techs as pt
                    ON        p.id = pt.project_id
                    JOIN      techs as t
                    ON        pt.techs_id = t.id
                    JOIN      images as i
                    ON        p.id = i.projects_id
                    WHERE p.slug =  :slug

The ER-diagram ER图 ER图

You are asking MySQL do something that related to presentation of data. 你要求MySQL做一些与数据表示有关的事情。 MySQL cannot show anything beside "rows". MySQL无法在“行”旁边显示任何内容。 It can make anything you can describe as a row, but it cannot do rows within rows - it simply can't. 它可以创建任何你可以描述为行的东西,但它不能在行内行 - 它根本不能。

I suggest to return your "rows" and then have PHP pack them as you wish, rather than work hard on some very complex SQL query that might get you the output you desire, but would probably be: 1) Inefficient 2) Very hard to maintain one year from now 我建议返回你的“行”,然后让PHP按你的意愿打包,而不是努力学习一些非常复杂的SQL查询,它可能会得到你想要的输出,但可能是:1)效率低2)很难从现在开始维持一年

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM