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


possible with query? or must use PHP organize this





Mgccl
Code:

function get_rank($id){
                $query = "SELECT user_id ".
      "FROM user_prof ".
      "ORDER BY exp DESC";
                $result = mysql_query($query) or die (mysql_error());
   $row = mysql_fetch_array($result) or die (mysql_error());
   //loop to get rank of the user
   $i = 0;
   
   $num_row = mysql_num_rows($result);
   
   while ($row['user_id'] != $id) {
      if($i >= $num_row){
         break;
      }
      $i++;
   }
return $i;
}



see what I did is
1. get all data in the database
2. use a while loop in PHP find the place of an id(because all the data are fetched and ordered)

so I used PHP to find the place of the id
but can MySQL do it?
like return only where the id will locate if order in somekind of way?
hexkid
Mgccl wrote:
so I used PHP to find the place of the id
but can MySQL do it?
like return only where the id will locate if order in somekind of way?


Yes, much better to do it on the MySQL side!
Code:
function get_rank($id) {
  $query = "SELECT count(user_id)
FROM user_prof
WHERE exp >= (
    SELECT exp
    FROM user_prof
    WHERE user_id=$id
)";
  $result = mysql_query($query) or die (mysql_error());
  $i = mysql_result($result, 0);
  mysql_free_result($result);
  return $i;
}


The code you posted has a bug.
The comparison in the while() loop
Mgccl wrote:
Code:
while ($row['user_id'] != $id) {
is always comparing the same values over and over again.
Mgccl
thx... wow.. you are a master...
so looks like.... maybe most of my data manipulate things can be used directly from the MySQL instead of PHP!.
thx a lot...
Related topics
Linux or Windows on your computer?!
Can l get a css document to
how many of the four cardinal virtues do you practice?
How Tough is PHP - Part II
php problem
If you have created a form on your site you can help...
Make A Simple Response: Earn 100 FRIH$ For Your Opinion!!!
PHP Code to HTML
Using PHP in error pages
send an email from internet...
PHP/Mysql Automated scheduled tasks
windows mobile programming and php, combination possible?
God exists - and here's the proof
301 Redirect
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.