SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Mysql Row Id

  1. #1
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mysql Row Id

    Wasn't sure whether to put this in MySQL or PHP but...

    I'm writing a high score list in php and I need to figure out the rank of the player. Here is the relavent code:

    Code:
            if(empty($_GET['skill']) || $_GET['skill'] == 'overall') {
                $mysqlQuery = @mysql_query("SELECT * FROM `highscores` ORDER BY `skilltotal` DESC LIMIT 0, 20", $mysqlConnect) or die("DB Select Error: ". mysql_error());
                $_GET['skill'] = 'overall';
            } else
                $mysqlQuery = @mysql_query("SELECT * FROM `highscores` ORDER BY `". $skillInfo[$_GET['skill']]['columnName'] ."` DESC LIMIT 0, 20", $mysqlConnect) or die("DB Select Error: ". mysql_error());
    And:

    Code:
                            <tr><th style="font-size: 1.2em; width: 33%;">Rank</td><th style="font-size: 1.2em; width: 33%;">Name</th><th style="font-size: 1.2em; width: 33%;">Level</th></tr>
    <?php
    
            while($row = @mysql_fetch_array($mysqlQuery))
                echo("                        <tr><td>0</td><td>". $row['username'] ."</td><td>". $row[$skillInfo[$_GET['skill']]['columnName']] ."</td></tr>");
    I am sorting the results of the query, and need to know how to get the id/index/whatever of the row of this query. IE: relating to the first query above:

    Code:
    Index    User     Level
    0          abc       99
    1          def        78
    2          ghi        63
    3          jkl         22
    The id column of my table won't help here....I know there's a way because I used to know...a bit rusty with this stuff since I haven't done it in about a year.

    Any help would be appreciated.
    - Saj

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    there is no such thing as a row id in MySQL. You could use user variables or you could add two lines to your PHP:

    Code:
    <?php
    $count = 1;
    while($row = @mysql_fetch_array($mysqlQuery)) {
      echo("<tr><td>$count</td><td>". $row['username'] ."</td><td>" .
      $row[$skillInfo[$_GET['skill']]['columnName']] ."</td></tr>");
      $count++;
    }
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  3. #3
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I thought there was. Oh well then. I suppose I'd have to do without it. I guess my idea wouldn't have worked anyway...Thanks for your help though
    - Saj

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    are you asking how to get the rank of a specific row?

    e.g. what is joe's position out of all users, based on his level?

    for this type of query, you do not want to return the entire table, append a rownumber, and then loop until you find joe

    i can give you the sql for this, if this is what you want
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •