开发者

change the format of the array returned from a sql stored procedure

开发者 https://www.devze.com 2023-03-20 03:14 出处:网络
I don\'t even know if what I would like is possible but here you go: I have a stored procedure returning an array with all the data I need but not in the format that I would like: I have

I don't even know if what I would like is possible but here you go: I have a stored procedure returning an array with all the data I need but not in the format that I would like: I have

a table USER

a table TYPE

a table USER_TYPE (foreign key user_id from USER, type_id from TYPE)

a table GAME

a table USER_GAME (foreign key user_id from USER, game_id from GAME)

And I have a query with JOINS:

SELECT user_name,type_name,game_name

FROM user u

INNER JOIN user_type ut
    ON u.user_id=ut.user_id

INNER JOIN type t
      ON ut.type_id=t.type_id

LEFT JOIN user_game ug
    ON u.user_id=ug.user_id

INNER JOIN game g
    ON ug.game_id=g.game_id

WHERE t.type_name = ?

...

So I get an array in the following format:

[0] => Array
    (
        [user_id] => 1
        [type] => type1
        [game] => game1
    )

[1] => Array
    (
        [user_id] => 1
        [type] => type1
        [game] => game2
    )

[2] => Array
    (
        [user_id] => 1
        [type] => type2
        [game] => game2
    )

etc...

but I would like the开发者_StackOverflow中文版 query to return an array such as

[0] => Array
    (
        [user_id] => 1
        [type] => type1
        [game1] => game1
        [game2] => game2
    )
[1] => Array
    (
        [user_id] => 1
        [type] => type2
        [game1] => game1
        [game2] => game2
    )

etc...

Thus listing all the games for one user in each array: I guess I would need to basically keep the join on type, get the various user_id from it and then get the games for each of those various user_id, and kind of merge all that. That would be great if that could be done inside the procedure itself.

I hope that wasn't unreadable, would be very grateful for any help. By the way if any of you could direct me towards good tutorials about more advanced sql queries 'cause I'm a bit short on that... Thank you very much!


An array is a PHP side structure, not MySQL. Your stored procedure will return a resultset and you will have to loop through the PHP array to fetch all games from each gamer.

0

精彩评论

暂无评论...
验证码 换一张
取 消