views:

44

answers:

2

I have a multidimensional array. The array itself is fine. My problem is that the script takes up monster amounts of memory, and since I'm running this on my MAMP install on my iBook G4, my computer freezes up. Below is the full script.

$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 10"; 
$result = mysql_query($query);
$posts = array();
while($row = mysql_fetch_array($result)){

            $posts[$row["id"]]['post_id'] = $row["id"];
            $posts[$row["id"]]['post_title'] = $row["title"];
            $posts[$row["id"]]['post_text'] = $row["text"];
            $posts[$row["id"]]['post_tags'] = $row["tags"];
            $posts[$row["id"]]['post_category'] = $row["category"];

foreach ($posts as $post) {
   echo $post["post_id"];
}

Is there a workaround that still achieves my goal (to export the MySQL query rows to an array)?

-Dylan

A: 

This is strange because you have set the limit of 10 already. Note that you are specifying associative array eg $row["title"] while using mysql_fetch_array function, add second parameter to it MYSQL_ASSOC or use mysql_fetch_assoc function instead:

while($row = mysql_fetch_assoc($result)){
   $posts[$row["id"]]['post_id'] = $row["id"];
   $posts[$row["id"]]['post_title'] = $row["title"];
   $posts[$row["id"]]['post_text'] = $row["text"];
   $posts[$row["id"]]['post_tags'] = $row["tags"];
   $posts[$row["id"]]['post_category'] = $row["category"];
}

You might want to use the array_chunk function to chunks from array and manipulate those chunks however you want.

Sarfraz
Thanks but it didn't do the job
Dylan Taylor
@Dylan Taylor: What error message if any do you get?
Sarfraz
It's not an error message - the page just never loads and the httpd process (the MAMP process) goes way up to 28mb and climbing. I have to stop the page load and force quit the process.
Dylan Taylor
A: 

If that code is verbatim, copied/pasted directly from your code, one thing that caught my eye (unless I am seeing things) is the absence of the closing '}' for the while loop???

Cheers, Alex

Yeah that was my typo. It's there in my code. Thanks for the heads-up!
Dylan Taylor