开发者

PHP Code Causes Server To Hang

开发者 https://www.devze.com 2023-02-12 19:03 出处:网络
I\'m hitting my server really hard because of a bug somewhere in the following code: $qqq = mysql_query(\"SELECT * FROM coupons WHERE dateExp > CURDATE()\");

I'm hitting my server really hard because of a bug somewhere in the following code:

    $qqq = mysql_query("SELECT * FROM coupons WHERE dateExp > CURDATE()");
$isExpired = array();
while($row = mysql_fetch_array($qqq))
{
    $isExpired[] = $row;
}
foreach($isExpired as开发者_开发问答 $exp)
{
    if($exp['id'] == $coupID)
    {
        $expiredConf = 1;
    }
}
if($expiredConf == 1)
{
    echo "<link rel='stylesheet' href='grey.css' type='text/css' />";
}
else
{
    echo "<link rel='stylesheet' href='coupStyle.css' type='text/css' />";
}

I've written code like this a hundred times, and I've compared it to my old examples, but I can't figure out what caused such a major problem.


I'm not sure how this will work with the surrounding code, but if this part doesn't influence any other areas, you can really condense it down a lot:

$result = mysql_result(sprintf("SELECT id FROM coupons WHERE id = %d AND dateExp > CURDATE()",
                               mysql_real_escape_string($coupID)));

if(!$result){
    $url = 'coupStyle';
} else {
    // Found result
    $url = 'grey';
}

echo '<link rel="stylesheet" href="'.$url.'" type="text/css" />';

If you don't need to select every column from the table, try to only put down one column, and if possible, the table's ID. Also, it is generally advised to avoid using * in select's, and to instead specify which columns you need.


perhaps you should change your code a bit, cause working with big arrays is always quite slow:

while($row = mysql_fetch_array($qqq))
{
    $isExpired[] = $row;
    if($row['id'] == $coupID)
    {
        $expiredConf = 1;
    }
}

apart from that, your code looks fine. how many records do you have in this recordset?


You can reduce above code lines, No need one extra foreach loop

while($row = mysql_fetch_array($qqq))
{
   $isExpired[] = $row;
   if (in_array($coupID,$row))
   {
     $expiredConf = 1;         
   }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消