Simple php causing high server load

Hello ,

I have been contacted by my hosting company … got the following email :

I apologize, but I was forced to suspend the script “public_html/…/count.php” as it was causing a high load on the server, and due to the nature of the problem, I forced to take immediate action for the health of the server.

my php/mysql only takes a Post variable and (get the record or update the record)

here is the simple php code :

function get_total() {

// get the total numbers
$sqlstr = @mysql_query("select total from total where id = 1");

if (@mysql_num_rows($sqlstr) != 0) {
    while ($row = @mysql_fetch_array($sqlstr)) {
                $num = number_format($row[total]);
                echo "<h1> $num  </h1>";

if($_POST['count'] == 1) { get_total(); mysql_close($link);}

 else if($_POST['count'] == 2) {

@mysql_query("UPDATE  total set total = total + 1 where id = 1") or die("ERROR: You are Going too Fast !!");



Anyone see a problem with the code above or anyway i can make it better ?

What i might be doing wrong : i’m calling this page using ajax by 2 way :

  • if a user click on a button (update the row = row + 1 )
  • every 15 seconds (just get the value)

the problem is that for every user on the page there is a process :

Running Proceses:
… 7296 0.0 0.0 8192 944 ? S 21:04 0:00 pure-ftpd (IDLE)
… 12396 0.0 0.1 27600 10808 ? SN 21:08 0:00 /usr/bin/php /home/…/public_html/…/count.php
… 14367 0.0 0.1 27600 10808 ? SN 21:09 0:00 /usr/bin/php /home/…/public_html/…/count.php
… 16388 0.0 0.1 27600 10808 ? SN 21:10 0:00 /usr/bin/php /home/…/public_html/…/count.php
… 18604 0.0 0.1 27600 10808 ? SN 21:12 0:00 /usr/bin/php /home/…/public_html/…/count.php
… 19409 0.0 0.1 27600 10812 ? SN 21:13 0:00 /usr/bin/php /home/…/public_html/…/count.php
… 19472 0.0 0.1 27604 11192 ? SN 21:13 0:00 /usr/bin/php /home/…/public_html/…/index.php
… 20767 0.0 0.1 27600 10808 ? SN 21:14 0:00 /usr/bin/php /home/…/public_html/…/count.php
… 21192 0.0 0.1 27604 11192 ? SN 21:14 0:00 /usr/bin/php /home/…/public_html/…/index.php
… 23153 0.0 0.1 27604 11188 ? SN 21:15 0:00 /usr/bin/php /home/…/public_html/…/index.php
… 24941 0.2 0.1 27604 11192 ? SN 21:17 0:00 /usr/bin/php /home/…/public_html/…/index.php
… 25401 1.2 0.1 27600 10812 ? SN 21:17 0:00 /usr/bin/php /home/…/public_html/…/count.php

and mysql process :

xxx.161.213.173 …com /count.php
xxx.161.213.173 …com /count.php
xxx.48.198.248 …com /count.php
xxx.48.198.248 …com /count.php
xxx.48.198.248 …com /count.php
xxx.51.25.143 …com /count.php
xxx.12.236.124 …com /count.php
xxx.12.245.160 …com /count.php
xxx.14.242.162 …com /count.php
xxx.14.242.162 …com /count.php
xxx.14.242.162 …com /count.php
xxx.200.196.48 …com /count.php
xxx.236.228.150 …com /count.php
xxx.150.159.212 …com /count.php
xxx.71.97.109 …com /count.php
xxx.71.97.109 …com /count.php
xxx.71.97.109 …com /count.php
xxx.71.97.109 …com /count.php
xxx.71.97.109 …com /count.php

Any Help would be appreciated

Thank You

function get_total() {
	$sqlstr = mysql_query("SELECT total FROM total WHERE id = 1");

	foreach (mysql_fetch_array($sqlstr) as $row) {
		$num = number_format($row['total']);
		echo "<h1> $num  </h1>";

if ($_POST['count'] == 1)  { 
} else if ($_POST['count'] == 2) {
	mysql_query("UPDATE total SET total = total + 1 WHERE id = 1") or die("ERROR: You are Going too Fast !!");

do you think that simple (if & foreach loop) might be causing the trouble ?

what about when i start a session will that make a new process in php ?

If you have other files included which I expect you do since you’re using mysql_close($link) then any one of those could be causing the slow down, from I can see, what you’re currently doing wouldn’t be a problem.

See if you can replicate the issue at a local/testing machine. If you can, then use xdebug to profile it and find out where the bottleneck is.

The other include is just the db info :

$link = mysql_connect($host, $user, $pass);

i also had this to prevent external calls :


// Prevent this file from being called Directly

if ($_SESSION['whatever'] != 'whatever&^%') { echo "ACCESS DENIED" ; die(); }


if  (($ref != "") && ($ref != "")) { echo "Access Denied"; die();}

i removed this code temporarily to see if this was causing the “processes”

still dident work

it used 25 of 25 allowed process

Where you use the foreach loop to fetch the results of the query, try changing it to a while loop.

it was a while loop then Ripe Suggested to use the foreach …

now i’m not using both of them … since i only have 1 row and only will have 1 row wrote the following code :

// get the total number
$sqlstr = mysql_query("select total from total where id = 1");

$row = mysql_fetch_array($sqlstr) ;
echo "<h1>". number_format($row['total']) ."</h1>";

it’s still causing “too much processes” … anyone know how in the server a process is counted ? or why a process is not stopped directly

I’m not sure maybe all this processes are from ajax ?

because the php page is being called from another page using ajax .

so whenever a user clicks the button this script is called using ajax or every 15 seconds automatically .

maybe i should remove the “close mysql connection” since the same user might click many time on a button instead of opening / closing mysql connection every time ?

I’m not sure about what creates processes, but I know using @ to suppress errors makes it take longer to execute. It has to set error level to none, then restore it after every line with @.

I would start by creating a test page with only the commands to connect, select the database, run the query and display that value. If that still causes process issues, then it must be something in the web host’s configuration.

If a basic query works without creating too many processes, I would then make it run multiple queries in the same file to test whether that puts too much load on the server. How many queries would depend on the traffic you receive and how much the rest of your web site queries the database. If you run into errors, you might need to reduce the number of queries to see just how many you can do at once. Maybe find a better database server or reduce the demand by increasing the time between refreshes or using caches.

If it works with many queries at once, then I would add components from the page causing errors until I found what was pushing it too far.

Hope this helps.

What is the purpose of the script? Is it to log user activity? Check your log files for any times with lots of activity from a single or small group of IP addresses which could possibly indicate a bot is being used, you mention in the OP in this thread that if a user clicks something, if there is a bot trying to access the site then it might possibly explain the hitting of the script.