Jump to content

[SOLVED] Allowed memory size of x bytes exhausted


Recommended Posts

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.

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???

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. :)

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.

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.

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.

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.

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....

  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!

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.

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.';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.