Fatal error: allowed memory size exhausted

I was running the following simple query:

SELECT * FROM users WHERE member = ‘Distributor’ and membernumber is null;

I was sending out an email to these people when I ran into the following error. Does anyone know why this happened or how I can fix it?

Thanks.

Rob

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 10487992 bytes) in /home3/recordau/public_html/oils/tools/groupEmail.php on line 191

Do you know how many rows this gets?

When I simulate this script, and don’t actually insert rows into the Notifications table, it successfully shows that it would email 2231 people.

The error is a PHP error, can you post the relevant script?

<?php 

$action    = $_REQUEST['action'];


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//Display the form and set criteria
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


if ($action == "setup") {


include 'db.inc.php'; 

$title = "Setup Group Email";
$today = date("Y-m-d");

include 'toolsHeader.php';

?>

<table align='center' border='0'cellpadding='10' cellspacing='0'>


<form action=groupEmail.php?action=send method=POST>


<table align="center" border ="1">

<TR>
<TH colspan='2' valign='top'>Send options for Group Email </TH>
</TR>

<TR>
<TD bgcolor='FFFFFF' width='100' align="right">Directions:</TD>
<TD bgcolor='FFFFFF'>Follow these steps.</TD>
</TR>


<TR>
<TD bgcolor='FFFFFF' width='100' align="right">Simulate?</TD>
<TD bgcolor='FFFFFF'><input type=checkbox name='senderIntention'></TD>
</TR>



<TR>
<TD bgcolor='FFFFFF' width='100' align="right">&nbsp;</TD>
<TD bgcolor='FFFFFF'>&nbsp;</TD>
</TR>

<TR>
<TD bgcolor='FFFFFF' width='100' align="right">Name:</TD>
<TD bgcolor='FFFFFF'><input type=text name=senderName size=40 maxlength=50 value='Rob'></TD>
</TR>

<TR>
<TD bgcolor='FFFFFF' width='100' align="right">Email:</TD>
<TD bgcolor='FFFFFF'><input type=text name=senderEmail size=40 maxlength=50 value='support@oil-testimonials.com'></TD>
</TR>

<TR>
<TD bgcolor='FFFFFF' width='100' align="right">Subject:</TD>
<TD bgcolor='FFFFFF'><input type=text name=senderSubject size=40 maxlength=50></TD>
</TR>

<TR>
<TD bgcolor='FFFFFF' width='100' align="right">Note:</TD>
<TD bgcolor='FFFFFF'>Each email that goes out will automatically address<br> the user by their first name.</TD>
</TR>

<TR>
<TD bgcolor='FFFFFF' width='100' align="right">Message:</TD>
<TD bgcolor='FFFFFF'><textarea name="senderMessage" cols="40" rows="10" class=form id="senderMessage"></textarea>
</TR>


<TR>
<TD bgcolor='FFFFFF' width='100' align="right">mySQL Query:</TD>
<TD bgcolor='FFFFFF'><textarea name="senderQuery" cols="40" rows="10" class=form id="senderQuery">select * from users where city in ('Las Vegas', 'Reno', 'Pahrump', 'Boulder City', 'Henderson', 'Mesquite', 'Laughlin', 'St. George',  'Tucson', 'Phoenix', 'Mesa', 'Gilbert', 'Chandler', 'Tempe', 'Los Angeles', 'Scottsdale');</textarea>
</TR>

<TR>
<TD width='100' >&nbsp;</td>
<TD><input name="Submit" type="submit" id="Submit" value="Send"></TD>
</TR>

</TABLE>




</form>

</table>


<?php

} // end if ($action == "setup")


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//Send out the email messages to the appropriate contacts
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

if ($action == "send") {

include 'db.inc.php'; 

$title = "Group Email Sent";

include 'toolsHeader.php';


$timeStamp = date("Y-m-d, g:i A");

list($y, $m, $d, $h, $mm, $ampm) = sscanf($timeStamp, "%4d-%2d-%2d, %2d:%2d %2s"); 
if($ampm == "pm")$h+=12;

$timeStamp = date("Y-m-d H:i:s",strtotime("$y-$m-$d $h:$mm"));


$senderIntention = $_REQUEST['senderIntention'];
$senderName        = $_REQUEST['senderName'];
$senderEmail    = $_REQUEST['senderEmail'];
$senderSubject     = $_REQUEST['senderSubject'];
$senderMessage    = $_REQUEST['senderMessage'];
$senderQuery    = $_REQUEST['senderQuery'];


$senderSubject    = addslashes($senderSubject);
$senderMessage    = addslashes($senderMessage);
$senderQuery    = stripslashes($senderQuery);


$result = mysql_query($senderQuery) OR die(mysql_error());

if ($row = mysql_fetch_array($result)) {

$groupMatches = 0;


do {  

$groupMatches++;

$recipientuID        =$row["uID"];
$recipientFirstName    =$row["firstname"];
$recipientLastName    =$row["lastname"];
$recipientEmail        =$row["email"];


$groupNames .= "$recipientuID - $recipientFirstName $recipientLastName<br>";

$body = "$recipientFirstName,

$senderMessage";


$today        = date("Y-m-d h:i:s");
$groupType    = "Group Email";


$body    = addslashes($body);


//print("The senderIntention is set to $senderIntention");

////////////////////////////////////////////////////////////////////////////////
// If the form checkbox was checked, send the email instead of simulating a send
////////////////////////////////////////////////////////////////////////////////

if (!$senderIntention == "on") {

$senderMessage    = addslashes($senderMessage);

$groupUpdate = "INSERT INTO notifications (date, priority, type, sender, recipient, subject, message) VALUES (

'$today', 
'3',
'$groupType',
'$senderEmail',
'$recipientEmail',
'$senderSubject', 
'$body')";

mysql_query($groupUpdate) OR die(mysql_error());

} // end if (!$senderIntention == "on")



} while($row = mysql_fetch_array($result)); //End DO loop



} // End contact fetch results


$body    = stripslashes($body);


///////////////////////////////////////////////////////////////////////
// Show success message on the webpage
///////////////////////////////////////////////////////////////////////

print("<TABLE align='center' cellpadding='20' cellspacing='1' border='1'>");

print("<TR>");
print("<TD>");


if ($senderIntention == "on") {

print("<p><font color='red'>Email messages SIMULATED</font>");
}

else {
print("<p><font color='red'>Email messages sent</font>");

}




print ("<p>Query: $senderQuery<br>");

print ("<p>Emails sent: $groupMatches<br>");

print ("<p>Users that received the email:<p>$groupNames");


print("</TD>");
print("</TR>");
print("</TABLE>");

} // end if ($action == "send")




include 'toolsFooter.php';

?>

Anyone out there that can help me pinpoint the problem?

Thanks!

Try commenting out blocks of code, which block when commented out doesn’t cause the out of memory error?

I have basically done that by using the simulate checkbox in the form. If that is checked, then the emails are not inserted into the Notifications table. This error message only appears when I do not simulate and try to really send out the email messages.

Thanks

Try not asking it for * and instead asking for the relevant fields only?

If one of the fields in the table is a Bio TEXT sized field, you could easily overwhelm the memory limit with the result that pulls 2000 records…

As an update, the script will work when I modify the query to limit the results to ten, like this:

SELECT firstname, email FROM users WHERE member = ‘Distributor’ and membernumber is null limit 10;

Why on earth can I only successfully send out ten emails before I get a dreaded fatal error?

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 10487992 bytes)

Please help.

Thanks

Have you tried doing the select query direct against MySQL via PHPMyAdmin? Any errors given when you do?

You’re ending up in some sort of loop that you’re not expecting. I’m wondering if this line isn’t what’s giving you grief…

mysql_query($groupUpdate) OR die(mysql_error());

Try changing it to something like
$r = mysql_query($groupUpdate) OR die(mysql_error());