PHP array taking up too much memory
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"];
foreac开发者_如何学Goh ($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
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
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.
精彩评论