Jump to content

Recommended Posts

mysql>SET @rank:=0;

mysql>SELECT rank, user FROM (SELECT @rank:=@rank+1 AS rank, id, user, written FROM stats ORDER BY written DESC) AS foo WHERE user='Kingy';

 

+------+-------+

| rank  | user    |

+------+-------+

|    4  | Kingy  |

+------+-------+

 

that works perfectly for what i want to do, but as u can see im running that from the mysql command prompt, how would i get that exact output, using php?

Link to comment
https://forums.phpfreaks.com/topic/87167-mysql-help/
Share on other sites

<?php

$query = "select * from stats order by written desc";
$query = mysql_query($query);
$rank = 0;
while($row = mysql_fetch_array($query)){
    $rank++;
    if ($row['user'] == "Kingy"){
      echo "You are ranked $rank;
       $rank = 0;   
       break;
   }
}
?>

well i have that and its works fine, what i want now is to have

 

echo "You are ranked $rank out of $total";

 

how would i go about getting the total, i'd say i'd use some sort of count?

 

Link to comment
https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445899
Share on other sites

U can use count and also u have to use where if for some specific record you want to do counting.

Or:

$num=mysql_num_rows($query);

also can use.

Then use if statement and give condition like:

 

if($num > 10) { do something }else {
blah blah }

 

Am i right to get your idea.

Link to comment
https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-445913
Share on other sites

mysql_num_rows($query) will give you the number of rows.  BTW, I suggest you don't use $query for both the query itself and the result, as they are different things.  Better to do like this:

 

$query = "select * from stats order by written desc";
$result = mysql_query($query);
if (!$result) die("Error in $query: " . mysql_error());
print "There were " . mysql_num_rows($result) . " rows returnd\n";
$rank = 0;
while($row = mysql_fetch_array($result)){
    $rank++;
    if ($row['user'] == "Kingy"){
      echo "You are ranked $rank;
       $rank = 0;   
       break;
   }
}

Link to comment
https://forums.phpfreaks.com/topic/87167-mysql-help/#findComment-448441
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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