kisazeky Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/ Share on other sites More sharing options...
Mchl Posted April 14, 2009 Share Posted April 14, 2009 Your script got into an infinite loop, and these tend to hog resources. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810150 Share on other sites More sharing options...
jackpf Posted April 14, 2009 Share Posted April 14, 2009 Error supression with @ is supposed to be costly when executing php. Only by milliseconds, but still. There's not really much point in doing it. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810154 Share on other sites More sharing options...
Maq Posted April 14, 2009 Share Posted April 14, 2009 Which loop did you add the $i++ into? There are 2 while loops.. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810156 Share on other sites More sharing options...
jackpf Posted April 14, 2009 Share Posted April 14, 2009 Yeah, and think about it - if $i = 0, and you're not incrementing it, it's always going to be 0. So the statement "while($i < 1)" will always return true. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810157 Share on other sites More sharing options...
taquitosensei Posted April 14, 2009 Share Posted April 14, 2009 plus you've got two $i loops so every loop you're overwriting what $i was with the 2nd loop. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810159 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 14, 2009 Share Posted April 14, 2009 $result and $i are used in 2 differents while loop one inside the other. It just can't work. Rename them in one the 2 loop, and post the full file for more help. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810160 Share on other sites More sharing options...
kisazeky Posted April 14, 2009 Author Share Posted April 14, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810169 Share on other sites More sharing options...
Mchl Posted April 14, 2009 Share Posted April 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810174 Share on other sites More sharing options...
Maq Posted April 14, 2009 Share Posted April 14, 2009 That shouldn't cause an infinite loop. Although, I don't get the point of a while loop when it's only going to loop once... Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810175 Share on other sites More sharing options...
Mchl Posted April 14, 2009 Share Posted April 14, 2009 See my edited post above Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810177 Share on other sites More sharing options...
wildteen88 Posted April 14, 2009 Share Posted April 14, 2009 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); } Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810179 Share on other sites More sharing options...
kisazeky Posted April 14, 2009 Author Share Posted April 14, 2009 Thanks, teen. I was modeling this off of someone else's code, so I believed you had to loop queries all the time. From now on I'll use what you showed me unless I really do need to loop data. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810251 Share on other sites More sharing options...
wildteen88 Posted April 15, 2009 Share Posted April 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154118-why-did-this-php-page-consume-almost-100-of-a-huge-servers-cpu-time/#findComment-810486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.