benphelps Posted August 25, 2008 Share Posted August 25, 2008 $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 More sharing options...
revraz Posted August 25, 2008 Share Posted August 25, 2008 If you only want 'totaltime', just grab totaltime instead of * $countchar = "SELECT totaltime FROM characters;"; Link to comment https://forums.phpfreaks.com/topic/121278-mysql-and-php-adding-a-returned-var/#findComment-625236 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 Or: #SQL SELECT SUM(totaltime) AS total FROM characters; Link to comment https://forums.phpfreaks.com/topic/121278-mysql-and-php-adding-a-returned-var/#findComment-625240 Share on other sites More sharing options...
Mchl Posted August 25, 2008 Share Posted August 25, 2008 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; } Link to comment https://forums.phpfreaks.com/topic/121278-mysql-and-php-adding-a-returned-var/#findComment-625241 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 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! 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']; Link to comment https://forums.phpfreaks.com/topic/121278-mysql-and-php-adding-a-returned-var/#findComment-625245 Share on other sites More sharing options...
benphelps Posted August 25, 2008 Author Share Posted August 25, 2008 missing ; but it works great, thanks guys i just noticed myself that it runs the query every time. Link to comment https://forums.phpfreaks.com/topic/121278-mysql-and-php-adding-a-returned-var/#findComment-625253 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 missing ; but it works great, thanks guys i just noticed myself that it runs the query every time. Yeah, I noticed the missing ; too. xD Woops. Link to comment https://forums.phpfreaks.com/topic/121278-mysql-and-php-adding-a-returned-var/#findComment-625254 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.