PHP页面永远加载

时间:2011-02-28 17:46:50

标签: php

我有一个页面在我改变了一些小的东西后永远加载,我不知道为什么。我使用Chrome的web dev实用程序检查了该页面,它说它无限尝试$_GET某些东西,但我的代码中根本没有这样的请求。我怎样才能尝试调试并找出导致问题的原因?

这是我认为负责的代码块:

<?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 = $newCoups[($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'"));
?>

5 个答案:

答案 0 :(得分:1)

do...while的目的是什么,如果你可以用if达到同样的简单效果? while是一个非常危险的陈述,在大多数情况下总会出现无限循环的情况。

答案 1 :(得分:1)

要获得随机x,您可以轻松地将id数组($ newCoups)洗牌并获取它们的前x个。这将产生正确的结果。在您的代码中,您生成3个随机ID,假设那些id位于该区域中(在某些时候可能会失败)。您可以使用sql来获取新的和随机的,这也是最有效的方式。

随机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

最新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

答案 2 :(得分:0)

将一些调试输出(例如echo "1";echo "2";)放入页面的不同部分。每次输出后刷新输出缓冲区(flush();)。然后,您将能够看到您的脚本在哪里挂起......

答案 3 :(得分:0)

对于语句而言,挂起页面通常会出错。尝试注释掉每个单独的while语句,将其缩小到一个循环。

答案 4 :(得分:0)

要稍微清理你的代码,并删除可能导致你的页面挂起的while循环,你可能想要研究使用MySQL的RAND(),以及INNER JOIN将这两个表连接在一起。您将能够显着清理代码,同时仍然保持您的功能。