AKor
AKor

Reputation: 8882

PHP Code Causes Server To Hang

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.

Upvotes: 0

Views: 246

Answers (3)

Shakti Singh
Shakti Singh

Reputation: 86386

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;         
   }

}

Upvotes: 0

Adam
Adam

Reputation: 2889

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.

Upvotes: 1

Alex
Alex

Reputation: 536

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?

Upvotes: 0

Related Questions