Web Hosting Talk







View Full Version : PHP Mysql remote running slow


leway21
07-13-2008, 09:51 PM
On my portal site, when I try and get some mysql stats remotely from my webhost to my server it takes around 30 secs to load the website.

http://r3volution.servegame.org/portal/

//stats for WoW
$con = mysql_connect("teamr3.servegame.com", "", "");
mysql_select_db("account", $con);

$sql1 = 'SELECT * FROM characters WHERE online = "1";';
$result1 = mysql_query($sql1);
$count1 = mysql_num_rows($result1);

$sql2 = 'SELECT * FROM characters;';
$result2 = mysql_query($sql2);
$count2 = mysql_num_rows($result2);

$sql3 = 'SELECT * FROM accounts;';
$result3 = mysql_query($sql3);
$count3 = mysql_num_rows($result3);

$online_wow=@fsockopen ('teamr3.servegame.com','80',$errno,$errstr, 0.5);
if($online_wow)
{$color = 'lime'; $wow_status = 'Online';}
else{$color = 'red'; $wow_status = 'Offline';}

Any idea why it is taking so long to load?

Krishopper
07-13-2008, 10:38 PM
Nothing stands out from the code. I would suggest putting some debugging lines in to time how long it takes between the sql statements (including before and after the initial mysql connection and database selection)

foobic
07-13-2008, 11:27 PM
You're pulling the entire contents of the characters table and the accounts table out of the database just to count how many items are in each.

As a start, change to using the count function, eg:
$sql3 = 'SELECT COUNT(*) FROM accounts;';
$result3 = mysql_query($sql3);
$row3 = mysql_fetch_row($result3);
$count3 = $row[0];

Then if your "online" field only contains 1 or 0 you could also use SUM(online) to return the online count, ie. the first 2 queries would be replaced with:
SELECT COUNT(*),SUM(online) FROM characters

leway21
07-14-2008, 01:09 AM
Yes that is the problem, but

$sql3 = 'SELECT COUNT(*) FROM accounts;';
$result3 = mysql_query($sql3);
$row3 = mysql_fetch_row($result3);
$count3 = $row[0];

Is returning nothing

When I run SELECT COUNT(*) FROM accounts as a query it returns the correct value but on the site it is returning nothing.

foobic
07-14-2008, 01:29 AM
My mistake - that last line should be:
$count3 = $row3[0];

leway21
07-14-2008, 01:47 AM
Thanks much, works great!

stone007
07-14-2008, 12:51 PM
I think you're selecting too much at a time