Jump to content

Why did this php page consume almost 100% of a huge server's CPU time?


kisazeky

Recommended Posts

I had an account suspended on a paid, shared web host recently.  They said it was because one of my php pages was taking up more than 10% CPU time.  What's shocking is that looking at the reports, it was nearly 10 times that!

 

Top Process %CPU 97.0 /usr/bin/php /home/digiado1/public_html/buy.php

Top Process %CPU 76.3 /usr/bin/php /home/digiado1/public_html/buy.php

Top Process %CPU 71.4 /usr/bin/php /home/digiado1/public_html/buy.php

 

Now I have a hunch as to why this happened.  Here's a bit of code from buy.php:

 

$query = "SELECT * FROM `users` WHERE `username` = '".$luser."'";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i=0;
while ($i < 1) {

$money =@mysql_result($result,$i,"money");

$query = "SELECT * FROM adoptables WHERE uid = '$aID'";
$result = @mysql_query($query);
$num = @mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$aID=@mysql_result($result,$i,"uid");
$name=@mysql_result($result,$i,"name");
$imageurl=@mysql_result($result,$i,"imageurl");
$cost=@mysql_result($result,$i,"description");

if($money < $cost){
$article_title = "Oops";
$article_content = "You don't have enough bits to afford $name. <a href='shop.php'>Go back</a>.";
}

else {

 

I noticed that when the conditions met that If statement, the page would time out.

 

So I added

 

$i++;
}

 

to close the output of both those queries, and now it loads fine.

 

So my question to you php freaks is:

 

Is it likely that not closing the output properly caused this much load on the CPU?

 

I don't get much traffic to my site. :P

 

 

Link to comment
Share on other sites

I did this:

 

$query = "SELECT * FROM `users` WHERE `username` = '".$luser."'";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i=0;
while ($i < 1) {

$money =@mysql_result($result,$i,"money");

$i++;
}

$query = "SELECT * FROM adoptables WHERE uid = '$aID'";
$result = @mysql_query($query);
$num = @mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$aID=@mysql_result($result,$i,"uid");
$name=@mysql_result($result,$i,"name");
$imageurl=@mysql_result($result,$i,"imageurl");
$cost=@mysql_result($result,$i,"description");

$i++;
}

 

Does this stop the infinite loop? 

Link to comment
Share on other sites

It does... You could however just as well remove both loops, as both of them run once only.

 

Edit:

 

It seems to me like you should replace

while ($i < 1) {

 

with

 

while ($i < $num) {

 

in both cases for this code to make (some) sense

Link to comment
Share on other sites

Looking at your code you do not need to use any form of loop. As you're only returning one result. You could of done it this way:

 

$query = "SELECT money FROM `users` WHERE `username` = '".$luser."'";
$result= mysql_query($query);
if(mysql_num_rows($result) != 0)
{
    list($money) = mysql_fetch_row($result);

    $query = "SELECT name, imageurl, description FROM adoptables WHERE uid = '$aID'";
    $result = mysql_query($query);

    list($name, $imageurl, $cost) = mysql_num_rows($result);
}

 

 

 

Link to comment
Share on other sites

Oops Just to let you know I had a typo in the code.

 

This line

list($name, $imageurl, $cost) = mysql_num_rows($result);

should of been

list($name, $imageurl, $cost) = mysql_fetch_row($result);

 

To clarify you only need to use a loop if you are planning on retrieving more than one result from the query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.