Jump to content

mysql and php, adding a returned var


benphelps

Recommended Posts

$count = "SELECT * FROM characters;";
while($row = mysql_fetch_array(mysql_query($count))){
$played = $row['totaltime']+$played;
}

 

This code is very slow, it loops adding the totaltime about 800 times.

 

Is there a faster method to do this, it currently takes about 6 min.

Link to comment
https://forums.phpfreaks.com/topic/121278-mysql-and-php-adding-a-returned-var/
Share on other sites

Also try running the query once instead of twice.

 

$countchar = "SELECT totaltime FROM characters;";
mysql_select_db('characters2');
$result = mysql_query($countchar);
$char2 = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
$played = $row['totaltime']+$played;
}

Omg, I didn't even notice that.  You run the query at least 800 times then because you put it in the loop!  You need to query before the loop so it gives the proper result set and saves memory! :o  Try:

 

$count = "SELECT SUM(totaltime) AS total FROM characters;";
$result = mysql_query($count) or die(mysql_error());
$row = mysql_fetch_array($result)
$played = $row['total'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.