SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Wheelersburg
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select each row ordering by name

    Hey guys,


    I have a table, with an ID row, some rows that I want to get information from(match1, match2, match3, etc. are their names..up to match30, although all these rows are basically pointless in this question), and also a lastname row..


    Now all I want to be able to do is go through the rows in order by last name..this means like a SELECT * FROM query where I can get each result individually..I'll try to give an example of this..

    PHP Code:
    $sql mysql_query("SELECT * FROM information WHERE grade='9' ORDER BY lastname asc");
        
    $numrows mysql_num_rows($sql);
        for (
    $i=0$i <= $numrows$i++)
        {
            while (
    $row mysql_fetch_array($sql))
             {
    // Ok, now would this code run through like 100 times if there were 100 rows from grade='9' ?
    // If so, then would the first row select the last name Aardvark first, etc in alphabetical order?
    }

    Sorry for not explaining well, but I hope you guys can understand..
    http://chaseschwalbach.com

  2. #2
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a table, with an ID row, some rows that I want to get information from(match1, match2, match3, etc. are their names..up to match30, although all these rows are basically pointless in this question), and also a lastname row..
    I think you have your terminology mixed up here.
    This is what I think you mean

    I have a table, with an ID column, some columns that I want to get information from (e.g. match1, match2, match3, etc. are their names..up to match30, although all these columns are basically pointless in this question), and also a lastname column ...

    to your code - you are just about there

    PHP Code:
     $sql 'SELECT'
          
    ' lastname'
          
    ' FROM'
          
    ' information' 
          
    ' WHERE'
          
    ' grade = 9'          /* I have assumed that grade is an integer field */
          
    ' ORDER BY'
          
    ' lastname ASC'
          
    ;
     
     
    /* ***** START DIAGNOSITC ***** */
     
    echo $sql '<br />';
     
    /* ***** END DIAGNOSITC ***** */
     
     
    $result  mysql_query($sql) or die ('Failed to execute ' $sql ' due to ' mysql_error());
     while (
    $row mysql_fetch_assoc($result))
     {
       echo 
    $row['lastname'] . '<br />';
     } 
    Hope this helps


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
  •