I wrote a PHP script that joins two tables to display information in this format:
Jane Doe
Phone: 082 980 9514
Home Loan Applications (Active) - 17/07/2013
Credit Report (Free Report) (Unsubscribed) 12/06/2013
You'll notice that the first and lastname is in the first line of the output, followed by the phone number and then the mailing lists they've been subscribed. Next to the list is the status. Active means that the user has not unsubscribed and is still active while the unsubscribed date is displayed if the user already opted out of the mailing list. Then it is followed by the registration date of the subscriber to a certain mailing list.
Here's the link to the sample tables: Tables
It works as it should but it takes forever to complete since there are about 76,000 records in 1 table and about 100,000 in the other. I would like to ask suggestions on how to optimize the code to speed up the script.
Here's the current code that I've written:
$resultarray = array();
$rs4 = mysqli_query($con1,"SELECT interspire_customfield.subscriberid, interspire_customfield.fname, interspire_customfield.lname, interspire_customfield.phone, emailaddress, subscribedate, unsubscribed, interspire_customfield.listid, listname FROM `interspire_subscriber` INNER JOIN `interspire_customfield` ON interspire_subscriber.subscriberid = interspire_customfield.subscriberid GROUP BY emailaddress");
while($row4 = mysqli_fetch_array($rs4)) {
$resultarray[] = $row4['subscriberid'].",".$row4['fname'].",".$row4['lname'].",".$row4['phone'].",".$row4['emailaddress'];
}
foreach ($resultarray as $arrlist) {
$arr = explode(',', $arrlist);
$sid = $arr[0];
$frstname = $arr[1];
$lstname = $arr[2];
$pnum = $arr[3];
$emailadd = $arr[4];
echo $frstname." ".$lstname."<br />";
echo "Phone: ".$pnum."<br />";
$rs5 = mysqli_query($con1,"SELECT interspire_customfield.subscriberid, subscribedate, unsubscribed, interspire_customfield.listid, listname FROM interspire_subscriber INNER JOIN interspire_customfield ON interspire_subscriber.subscriberid = interspire_customfield.subscriberid WHERE interspire_subscriber.emailaddress = '$emailadd' GROUP BY interspire_subscriber.listid");
if (!$rs5) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row5 = mysqli_fetch_array($rs5)) {
$listname = $row5['listname'];
$subdate = $row5['subscribedate'];
$unsub = $row5['unsubscribed'];
if($unsub == "0"){
$stat = "Active";
}else{
$stat = date('d/m/Y', $unsub);
}
$subdt = date('d/m/Y', $subdate);
echo "* $listname ($stat) - $subdt <br />";
}
echo "<br />";
}
WHEREclauses, instead of doing them implicitly in a loop.SELECTquery in each step of yourforeach?echoat the end. But that speed increase might be marginal and you could experience a blank page untill the script is done