0

I have the following tables, projects:

+----+----------+-------+------------+
| id | name     | admin | timestamp  |
+----+----------+-------+------------+
|  1 | Group1   |     2 | 1539678214 |
|  2 | Test     |     2 | 1539678214 |
+----+----------+-------+------------+

And users:

+----+----------------+------------------------+
| id | name           | email                  |
+----+----------------+------------------------+
|  1 | FOO            | [email protected]            |
|  2 | BAR            | [email protected]            |
|  3 | G2W            | [email protected]            |
+----+----------------+------------------------+

I run the following SQL command:

SELECT projects.id,projects.name,users.id as userid,users.email 
FROM projects 
INNER JOIN users ON users.id=projects.admin;

And I get the following result:

+----+----------+--------+-----------------+
| id | name     | userid | email           |
+----+----------+--------+-----------------+
|  1 | Group1   |      2 | [email protected]     |
|  2 | Test     |      2 | [email protected]     |
+----+----------+--------+-----------------+

And this is perfect, it's just what I need. Now my question is, what is the best way to do this in PHP and get the result in a bi-dimensional array.

This is what I have so far, but I get the result in a one-dimensional array, which i don't want:

$stmt = $_DB->prepare("SELECT projects.id,projects.name,users.id as userid,users.email 
                       FROM projects 
                       INNER JOIN users ON users.id=projects.admin");
if($$_DB->error) die($$_DB->error);

$stmt->execute();
$res=$stmt->get_result();
while($result[]=$res->fetch_array(MYSQLI_ASSOC)){}
print_r($result);

Result:

[0] => Array
    (
        [id] => 1
        [name] => Group1
        [userid] => 2
        [email] => [email protected]
    )

[1] => Array
    (
        [id] => 2
        [name] => Test
        [userid] => 2
        [email] => [email protected]
    )

This is what i want:

[0] => Array
    (
        [id] => 1
        [name] => Group1
        [admin] => Array
            (
                [id] => 2
                [email] => [email protected]
            )

    )

[1] => Array
    (
        [id] => 2
        [name] => Test
        [admin] => Array
            (
                [id] => 2
                [email] => [email protected]
            )

    )
8
  • 2
    There is no ready way to accomplish what you want. You'd need to write the code youself to interpret the results in whatever structure you see fit. Commented Oct 19, 2018 at 8:27
  • How does laravel do it? Don't they use a plugin or something? Commented Oct 19, 2018 at 8:28
  • 1
    The same way the framework does everything else. (E.g. interpreting query parameters.) Just by writing the appropriate code to implement the desired functionality. Commented Oct 19, 2018 at 8:29
  • 1
    @131 Laravel uses Eloquent ORM. ORM do it in PHP layer. Querying on RDBMS like MySQL will always give you a tabular format data ( array of arrays) - second level array being individual row Commented Oct 19, 2018 at 8:30
  • 1
    Just do a foreach to rebuild your result as you want? Or maybe (but it will be "ugly" : do a SELECT concat(...) and build a JSON format string to get your result and just do a json_decode after...but the foreach should be better ! Commented Oct 19, 2018 at 8:32

1 Answer 1

2

Query results will always be in a Tabular format (rows and columns). Basically, it will be an array of arrays, wherein second-level array represents a particular row. If you want your results to be in Multi-dimensional array, you will have to do this using PHP code, after getting query output:

$res=$stmt->get_result();

// Initialize result variable
$result = array();

// Get results row by row
while( $row = $res->fetch_array(MYSQLI_ASSOC) ) {

    // In a temp variable, create the multi-dim array as you wish
    $temp['id'] = $row['id'];
    $temp['name'] = $row['name'];
    $temp['admin'] = array('id' => $row['userid'], 
                           'email' => $row['email']);

    // insert the temp result row
    $result[] = $temp;
}
print_r($result);
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.