Click to See Complete Forum and Search --> : Memory Optimization


mrtblt
07-02-2008, 02:38 AM
I send my customers periodical e mails by using the foolowing script


$sql= mysql_query("select * from firmalar where cat in (1,2,3,4)") or die(mysql_error());
while ($row = mysql_fetch_object($sql))
{

$sql3= mysql_query("select * from crmtrack where mid='$row->id'") or die(mysql_error());
$num_rows=mysql_num_rows($sql3);
if ($num_rows==0)
{
$de_trimis=$row2->cont3;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: xx@xxxx.com' ."\r\n";
$subiect ='asdasdasd';
$destinatar = 'office@asdasdacom';
mail($destinatar, $subiect, $de_trimis, $headers);
echo $row->adi.' mail gonderildi <br>';
}


My question is this :

This loop causes a memory problem on server side? Or put a heavy borden on server? How i can optimize this script?

NogDog
07-02-2008, 03:29 AM
You should be able to do one query with an inner join to get the info from the database, which should save some time. (Ensuring the tables are properly indexed may help, too.)

I suspect your biggest performance hit though might be using mail() in a loop like that. It's not very efficient, and as stated on the manual page (http://www.php.net/manual/en/function.mail.php):
Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
For the sending of large amounts of email, see the » PEAR::Mail (http://pear.php.net/package/Mail), and » PEAR::Mail_Queue (http://pear.php.net/package/Mail_Queue) packages.