FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


My PHP enlightment(just happened today)





Mgccl
1st, I have to thank hexkid, else I don't know when I could know something as cool as this(maybe when I brought some book called "BIBLE OF BECAME SUPER-GRANDMASTER PHPER")
untill yesterday I thought about the MySQL query that I often use PHP to do.. I NEVER thought about MySQL can be that strong...
for instance, here is something I made that I totally never going to do it anymore

first, consider there is a table, contains

country_id | earning | time

what I need is to get a page that shows Each country's total earning(50 countries... each in average did about.... 5000 earning... so the database is have about 250000 rows)

what I did is, first query that turns out to be
$country[$i]['id']
$country[$i]['earning']
$i is a variable... interger
so... what I need to do now? I want to convert it into a format that became
$country[$i]['id']
$country[$i]['total_earning']
so I made a PHP loop that is going to loop though all 250000 data for 50 times....
then I optimized it. by make the loop stop when the next array's id is different. which looks like... only need to check like... 5 milion times. a lot faster...
But. I know. certainly. It's STILL slow! how to make it faster? from what I see. that's the FASTEST PHP can do(in my knowledge... I can't think any faster way)

then... I saw this code
Code:
SELECT COUNT(user_id)

and a then I saw a subquery inside a MySQL query!!!
so I go and studied about it.. a NEW world come into my mind! My GOD.. my script can be done in a much simpler way!
I got EVERYTHING!

so now I made it MILIONS of time faster by let MySQL do the job... here became the whole code I have used!

Code:
function func_select_country(){
   $query = "SELECT SUM(stat_elo), country_id
         FROM user_stat
         LEFT JOIN user_prof
         ON stat_id = user_id
         GROUP BY country_id ";
   $result = mysql_query($query) or die (mysql_error());
   $i = 0;
   while($row = mysql_fetch_array($result)){
      $country[$i]['id'] = $row['country_id'];
      $country[$i]['elo'] = $row['SUM(stat_elo)'];
      $i++;
   }
   $return ($country);
}


This is super cool. and strangely after 6 month of using PHP.. yesterday is the first day I know this is possible.
so I don't know if other people knows.. that's why I posted it here.
Related topics
is it allowed ?
A tribute to 9/11 Victims..
Tiger x86 set up and runing
The truth about oil...
what happen to frihost today
Explorer using 99% CPU time
Data recovery - last chance (Server 1)
The Middle East Conflict
every musilman is dreams to live in peace or to die trying
500- Internal Server Error - DONT START NEW THREADS
What Happened Today in 1794
Hip hop is dead
How Many Guys Love to Post?
Session directory permissions issue
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.