Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php mysql query row number

Options
  • 08-01-2006 4:46pm
    #1
    Registered Users Posts: 673 ✭✭✭


    Hey,

    Im doing a mysql query with a "where" and "ORDER BY ..... DESC" clause. When i get the result i also want to be able to say which row this results came from.

    I want the query to tell me what row the result has come from i.e. if the results from this query is the 13th from the top (after they have been ordered descendingly), i want to be able to echo this out.
    $query = "SELECT points_total_".$game." as total_points
    		FROM apple_users
     		WHERE user_id = '".$_SESSION['user_id']."'
    		ORDER BY total_points DESC
    		";
    		$results = mysql_query($query)
    			or die(mysql_error());
    
    		while ($rows=mysql_fetch_assoc($results)) {
    		extract ($rows);
    

    Anyone know what i would have to add to the following query to do this?

    Thanks


Comments

  • Registered Users Posts: 3,182 ✭✭✭dionsiseire


    unless the user have an id number ( to which hopefully your database it storing the tables in order of ID) theres not much point

    the ID generated obviously can be printed out. but if you have a database with 50 entries and the query returns 1 item and you want to know what row it came from then just print the index

    alternativly if you mean you wish you run a query and beside the result of ( say 20 results from the 50) so an index number beside the results
    i.e.
    1 | row 1
    2 | row 2
    down to 20

    i think you just need to print an index variable before the results and increment as you loop

    i may have picked this up wrong though


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Ok, im a total PHP newbie, how do i print the index? :rolleyes:


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    Ok, im a total PHP newbie, how do i print the index? :rolleyes:
    Just start a variable say $reccount=0 once you have a result set, and increment it inside the while loop. Then you can echo it out with each line.
    I can't recall if there's a pointer maintained as you while your way through the result set...


Advertisement