For each result in MySQL query, push to array (complicated)

Posted by Dylan Taylor on Stack Overflow See other posts from Stack Overflow or by Dylan Taylor
Published on 2010-06-15T18:20:27Z Indexed on 2010/06/15 18:22 UTC
Read the original article Hit count: 160

Okay, here's what I'm trying to do. I am running a MySQL query for the most recent posts. For each of the returned rows, I need to push the ID of the row to an array, then within that ID in the array, I need to add more data from the rows. A multi-dimensional array.

Here's my code thus far.

$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 10"; 
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

            $id = $row["id"];
            $post_title = $row["title"];
            $post_text = $row["text"];
            $post_tags = $row["tags"];
            $post_category = $row["category"];
            $post_date = $row["date"];



}

As you can see I haven't done anything with arrays yet. Here's an ideal structure I'm looking for, just incase you're confused.

The master array I guess you could call it. We'll just call this array $posts. Within this array, I have one array for each row returned in my MySQL query. Within those arrays there is the $post_title, $post_text, etc.

How do I do this? I'm so confused.. an example would be really appreciated.

-Dylan

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql