0

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

2
  • How much memory is being used? Have you measured using memory_get_usage? Commented Jun 16, 2010 at 3:32
  • 2
    It can't possibly be a problem with a syntax error caused by the missing } after the loop which doesn't get reported since error reporting is turned off, can it? :) Commented Jun 16, 2010 at 3:34

2 Answers 2

1

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

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

1 Comment

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

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.

2 Comments

Thanks but it didn't do the job
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.

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.