I have a page that keeps loading forever after I changed something minor, and I'm not sure why. I inspected the page with Chrome's utility for web dev and it said it was infinitely trying to $_GET
something, but there is no such request in any of my code at all. How else could I try to debug and figure out what is causing the problem?
This is the block of code that I believe is responsible:
<?php
$con = mysql_connect(...);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(db, $con);
$thisZone = $_SESSION["zoneSelected"];
$newSQL = mysql_query("SELECT id FROM coupons WHERE zone ='$thisZone'");
$numIDs = mysql_num_rows(mysql_query($newSQL));
$newCoups = array();
while($row = mysql_fetch_array($newSQL))
{
$newCoups[] = $row;
}
//generates the 3 random
$randID1 = mt_rand(1, $numIDs);
do {
$randID2 = mt_rand(1, $numIDs);
}
while(in_array($randID2, array($randID1)));
do {
$randID3 = mt_rand(1, $numIDs);
}
while(in_array($randID3, array($randID1,$randID2)));
$randArr1 = mysql_fetch_array(mysql_query("SELECT retailerName,savingsDetails,dateExp,qrPicture FROM coupons WHERE id = '$randID1' AND zone = '$thisZone'"));
$randStats1 = mysql_fetch_array(mysql_query("SELECT views,saves,QRScans,prints FROM stats WHERE id = '$randID1'"));
$randArr2 = mysql_fetch_array(mysql_query("SELECT retailerName,savingsDetails,dateExp,qrPicture FROM coupons WHERE id = '$randID2' AND zone = '$thisZone'"));
$randStats2 = mysql_fetch_array(mysql_query("SELECT views,saves,QRScans,prints FROM stats WHERE id = '$randID2'"));
$randArr3 = mysql_fetch_array(mysql_query("SELECT retailerName,savingsDetails,dateExp,qrPicture FROM coupons WHERE id = '$randID3' AND zone = '$thisZone'"));
$randStats3 = mysql_fetch_array(mysql_query("SELECT views,saves,QRScans,prints FROM stats WHERE id = '$randID3'"));
//generates the 3 new
$coupsThisZone = count($newCoups);
$newID1 = $newCoups[($coupsThisZone - 1)];
$newID2 = $newCoups[($coupsThisZone - 2)];
$newID3 = $newCo开发者_运维技巧ups[($coupsThisZone - 3)];
$newArr1 = mysql_fetch_array(mysql_query("SELECT retailerName,savingsDetails,dateExp,qrPicture FROM coupons WHERE id = '$newID1' AND zone = '$thisZone'"));
$newStats1 = mysql_fetch_array(mysql_query("SELECT views,saves,QRScans,prints FROM stats WHERE id = '$newID1'"));
$newArr2 = mysql_fetch_array(mysql_query("SELECT retailerName,savingsDetails,dateExp,qrPicture FROM coupons WHERE id = '$newID2' AND zone = '$thisZone'"));
$newStats2 = mysql_fetch_array(mysql_query("SELECT views,saves,QRScans,prints FROM stats WHERE id = '$newID2'"));
$newArr3 = mysql_fetch_array(mysql_query("SELECT retailerName,savingsDetails,dateExp,qrPicture FROM coupons WHERE id = '$newID3' AND zone = '$thisZone'"));
$newStats3 = mysql_fetch_array(mysql_query("SELECT views,saves,QRScans,prints FROM stats WHERE id = '$newID3'"));
?>
What is the purpose of do...while
there, if you can achieve the same simple effect with if
? while
is a very dangerous statement and in most cases there is always a situation where you get infinite loops.
To get random x you can easily shuffle the id array ($newCoups) and get the first x of them. which would yield the correct result. In your code you generate 3 random ids assuming that those id's is in that zone (which would possibly fail at some point). You can use sql to get both new and random aswell which would be the most efficient way.
Random 3:
SELECT c.id, c.retailerName,c.savingsDetails,c.dateExp,c.qrPicture, s.views,s.saves,s.QRScans,s.prints
FROM coupons c
LEFT JOIN stats s ON c.id = s.id
WHERE c.zone = '$thisZone'
ORDER BY RAND()
LIMIT 3
Latest 3 :
SELECT c.id, c.retailerName,c.savingsDetails,c.dateExp,c.qrPicture, s.views,s.saves,s.QRScans,s.prints
FROM coupons c
LEFT JOIN stats s ON c.id = s.id
WHERE c.zone = '$thisZone'
ORDER BY c.id DESC
LIMIT 3
A hanging page will often be down to erroneous while and for statements. Try commenting out each individual while statement to narrow it down to a single loop.
Put some debugging outputs(e.g. echo "1";
, echo "2";
) in the different parts of your page. Flush the output buffer( flush();
) after each output. Then you'll be able to see where exactly your script hangs...
To clean up your code a little bit, and to remove the while loops that might be causing your page to hang, you might want to look into using MySQL's RAND(), in addition to INNER JOIN'ing those two tables together. You will be able to clean up your code significantly, while still maintaining the functionality you are going for.
精彩评论