3

I'm trying to have mysql return column names that will be parsed by php into an array. Consider this:

$sql = "SELECT user.id AS `user[id]`, user.name AS `user[name]`,
            user_info.address AS `user[info][address]`
            user_info.zip AS `user[info][zip]`
        FROM user JOIN user_info ON user.id = user_info.id";
$stmt = $conn->query($sql);
$results = $stmt->fetchAll();

I want $results to look something like this:

array(
    [0] => array(
        [id] => 2,
        [name] => 'john',
        [info] => array(
            [address] => '1234 Main st',
            [zip] => 12345
        )
    )
    [1] => array(
        [id] => 3,
        [name] => 'tom',
        [info] => array(
            [address] => '2811 Second Ave.',
            [zip] => 98765
        )
    )
)

Is there any way to do this without manually looping through the results. I want functionality similar to how php handles square brackets in form submissions (ex. index.php?user[id]=2&user[name]=tom)

2
  • Duplicate of stackoverflow.com/questions/5111646/… Commented Dec 19, 2012 at 0:25
  • @Tyron Nope, taht actually goes through building it from some logic, all the logic is in this query, I just want PHP to do it's thing on it Commented Dec 19, 2012 at 0:27

1 Answer 1

0

No. SQL doesn't work this way, and neither the MySQL library nor its PHP bindings support this. If you want to have something similar to this then you'll want a true object-oriented database along with its PHP bindings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.