9three Posted February 12, 2009 Share Posted February 12, 2009 hey, I'm getting this error: Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php on line 53 Line 53 from that file contains $array[] = $field. Here is the whole method: public function fetchArray() { $this->fetchArray = mysql_fetch_assoc($this->query); if (!$this->fetchArray) Throw New Exception(mysql_error()); else { $array = array(); while ($field = $this->fetchArray) { $array[] = $field; } return $array; } } This is my index: <?php require('library/includes.php'); error_reporting(E_ALL|E_STRICT); ini_set('display_errors', 'on'); try { $connection = new mysql('localhost', 'root', ''); $connection->select('cms'); $query = "SELECT ID, Username, Email FROM admins"; $connection->query($query); $fields = $connection->fetchArray(); foreach($fields as $field) { $id = $field['ID']; echo $id; } } catch (Exception $e) { echo $e; } ?> Not sure what this error is as it's the first time I get this. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/ Share on other sites More sharing options...
alphanumetrix Posted February 12, 2009 Share Posted February 12, 2009 this is on your computer? i've never done that before; i always test on my server. the space you've set up on your computer has probably ran out. you need to remove some files. my guess why it says "line 53" is because that's as far as it can read, due to a data restriction. that's all i've got on this one??? Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760900 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 Yes its running on a localhost, my laptop. I just deleted some trash files though but nothing changed. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760901 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 Open up your php.ini look for "memory_limit" change that to be a higher number, restart apache and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760905 Share on other sites More sharing options...
corbin Posted February 12, 2009 Share Posted February 12, 2009 You can also set it with ini_set. ini_set("memory_limit","xM"); Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760909 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 Its currently set at 32M which equals 16MBs. Should this really require so much? I don't ever remember having to change around this setting. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760910 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 I think I spotted the issue: public function fetchArray() { $this->fetchArray = mysql_fetch_assoc($this->query); if (!$this->fetchArray) Throw New Exception(mysql_error()); else { $array = array(); // this will not work. while ($field = $this->fetchArray) do { $array[] = $this->fetchArray; }while ($this->fetchArray = mysql_fetch_assoc($this->query)); // this should. return $array; } } Although I am not sure which way you want it, but your logic is flawed there. The problem was an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760913 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 I'm not sure if that's entirely correct seeing that $this->fetchArray already contains mysql_fetch_assoc. And in the while you actually make it equal the samething. But I see what you're trying to accomplish ill give it a try now. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760917 Share on other sites More sharing options...
corbin Posted February 12, 2009 Share Posted February 12, 2009 Uh, yes it is most definitely an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760921 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 I'm not sure if that's entirely correct seeing that $this->fetchArray already contains mysql_fetch_assoc. And in the while you actually make it equal the samething. But I see what you're trying to accomplish ill give it a try now. mysql_fetch_assoc look at the examples. Since you grab the first row with the first line of that function, the do while will run with that first row, then each successing loop will iterate through the rows. That code there will work, guaranteed by me. If it is how you want it coded is another issue. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760922 Share on other sites More sharing options...
9three Posted February 13, 2009 Author Share Posted February 13, 2009 lol it worked. nice job catching that one, was a bit tricky ty EDIT: Although I'm still confused though. I don't understand how it was an infinite loop. If you take this example from php.net: while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } It's exactly the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760927 Share on other sites More sharing options...
corbin Posted February 13, 2009 Share Posted February 13, 2009 No, it's not. mysql_fetch_assoc eventually returns false when it runs out of rows. That statement is basically equivalent to: while(($row = mysql_fetch_assoc($result)) != false) Note that it's = by the way, and not == with the $row = part. You're basically doing this: $x = something; while($y = $x) { } So, as long as $x is true, that loop will continue, and $x will not change unless changed in the while block. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760937 Share on other sites More sharing options...
9three Posted February 13, 2009 Author Share Posted February 13, 2009 So this is what I did: public function fetchArray() { $this->fetchArray = mysql_fetch_assoc($this->query); if (!$this->fetchArray) Throw New Exception(mysql_error()); else { return $this->fetchArray; $array = array(); //do //{ // $array[] = $this->fetchArray; //} //while ($this->fetchArray = mysql_fetch_assoc($this->query)); //while ($rows = mysql_fetch_assoc($this->query)) while ($rows = $this->fetchArray) { $array[] = $rows; } return $array; } } It also works, and it's coded to my flavor But I'm noticing that its adding an extra 2 strings at the end. Currently, if you notice from my Index page, I'm only pulling the ID. There is only one record, for now, in the database, and the ID = 1. In the index viewed from the browser, it shows as: 1Yy The heck? first time I see that too.... Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760942 Share on other sites More sharing options...
corbin Posted February 13, 2009 Share Posted February 13, 2009 return $this->fetchArray; Uhh..... your while loop will never be reached! And you still have an infinite loop x.x. Setting a variable to a variable doesn't make sense in a while condition. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760944 Share on other sites More sharing options...
9three Posted February 13, 2009 Author Share Posted February 13, 2009 public function fetchArray() { $this->fetchArray = mysql_fetch_assoc($this->query); if (!$this->fetchArray) Throw New Exception(mysql_error()); else { return $this->fetchArray; $array = array(); while ($this->fetchArray) { $array[] = $this->fetchArray; } return $array; } } I updated it and removed the variable declaration in the while. I don't get it! Why is it a infinite loop! I read what you posted too! Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-760949 Share on other sites More sharing options...
premiso Posted February 13, 2009 Share Posted February 13, 2009 Ok dude, look at what is changed. Here is the proper way to run your query. public function fetchArray() { // $this->fetchArray = mysql_fetch_assoc($this->query); No this is bad as it only gets 1 ROW. Note 1 ROW $this->numRows = mysql_num_rows($this->query); // check that we have rows returned. If not we are wasting time. if ($this->numRows < 1) { //Throw New Exception(mysql_error()); we do not want to throw the exception, cause any errors should be thrown via the query return array(); // instead return an empty array. Just because we did not get any rows returned, does not mean it is a bad query. }else { $array = array(); // instead of using $this->fetchArray, lets use $row as it may be easier to understand // that you are ITERATING (or moving) through each row that is returned. // Notice that each time the loop is ran it assigns a new row from the query to $row. // once all the rows have been iterated through, or looped through // mysql_fetch_assoc will return false thus $row is not set hence ending the loop. while ($row = mysql_fetch_assoc($query)) { $array[] = $row; // this stores the row in an array } return $array; } } Read the comments thoroughly to see why your logic is flawed and how you are using this completely wrong. Hopefully this helps you out. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-761032 Share on other sites More sharing options...
premiso Posted February 13, 2009 Share Posted February 13, 2009 Dangit, meant to do an edit. Please disregard my un-mindful clicking. Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-761043 Share on other sites More sharing options...
corbin Posted February 13, 2009 Share Posted February 13, 2009 9three, you must be overcomplicating this in your head..... It's really very, very simple. In fact, while clauses are one of the main concepts of any programming language x.x. Ok, first off, how a while clause works: while(<condition>) { //code } The code in the while block would continue to be executed while condition is true. So, how is condition determined to be true or not? Shortly and horribly put, anything that is not FALSE is assumed to be TRUE. (0 evaluates to FALSE.) 0 is false, FALSE is false, 1 is true, TRUE is true, 10 is true, -3 is true.... So on. Anything not 0, FALSE or NULL pretty much. Now, when there is a compound condition, everything is evaluated accordingly. As I'm sure you know by now, &&, || and so on combine statements. So, if you had 0 || 1, it would evaluate to true because 1 is true. So, what happens when you have just a variable? The content of the variable is casted to a boolean value (true/false). So, if you had $x = 5, and you had if($x), it would evaluate to true, and the code after the if statement would execute. Now, if you have: $this->fetchArray = mysql_fetch_assoc($this->query); if (!$this->fetchArray) Throw New Exception(mysql_error()); else { return $this->fetchArray; $array = array(); while ($this->fetchArray) { $array[] = $this->fetchArray; } return $array; } $this->fetchArray is what will be evaluated. mysql_fetch_assoc returns 2 things: either an array, or a boolean value of FALSE. An array evaluates to true, and FALSE obviously evaluates to false. Now, by the time the while statement is reached, it is safe to conclude that mysql_fetch_assoc returned an array, since if it returned FALSE, an exception would have been thrown. So, $this->fetchArray will evaluate to TRUE. And it will ALWAYS evaluate to true unless it is set to something else. Infinite loop: $i = 0; while($i < 10) { echo 'Hello'; } Terminating loop: $i = 0; while($i < 10) { echo 'Hello'; ++$i; } See why the terminating one is terminating? The variable involved in the condition is changed. So, what in the world does while(var = val) do? Well, in the case of $var = value, the return of the statement (which is always value) is evaluated to true or false. So, for example: <?php var_dump((bool) $x = 3); var_dump((bool) $x = array()); var_dump((bool) $x = false); Will output: bool(true) bool(true) bool(false) Now consider this: while($row = mysql_fetch_assoc($result_resource)) { //do something with $row } That will terminate once mysql_fetch_assoc returns false instead of an array. Your code, however, is just using the variable, unchanged, over and over again. Your code has this ideology: while($x = 5) { echo 'The code here is executed because, omg, x is 5.'; } Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-761064 Share on other sites More sharing options...
9three Posted February 13, 2009 Author Share Posted February 13, 2009 Looks like I was over complicating it in my head. Thanks for the detailed responses Quote Link to comment https://forums.phpfreaks.com/topic/145007-solved-allowed-memory-size-of-x-bytes-exhausted/#findComment-761307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.