Ordering Problem with PHP and MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peeHpee
    New Member
    • Feb 2008
    • 5

    Ordering Problem with PHP and MySQL

    Hello PHP Peoples! I'm having a bit of a problem, probably simple, but I can't seem to find any info on how to fix it. PHP version is 4.4.7 and MySQL version is 4.0.27.

    So here's the problem. Let's say I have 12 rows of data when I grab the rows from the DB with this code:

    Code:
    $sql = "select * from infop2 order by field_id2";
    $result = mysql_query($sql,$connection);
    $rows = mysql_num_rows($result);
    for ($r = 1; $r <= $rows; $r++) {
    $row = mysql_fetch_array($result);
    extract($row);
    The column "field_id2" is the primary key. Rows 10, 11, and 12 are displayed right after Row 1 instead of Row 9. I've only been working with PHP for about five years but I've made some pretty large scheduling programs and such and I've never encountered this before. Any ideas?

    I have been experimenting with this and have discovered that when I create a record with a 20-29 for the "field_id2" value that then displays after row 2.
  • peeHpee
    New Member
    • Feb 2008
    • 5

    #2
    OK fixed it. Moderators please erase this if you like.

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      By row I take that you mean field_id2.
      The actual database table row numbers are meaningless
      OK you have ordered by field_id2
      Code:
      select * from infop2 order by field_id2
      And as expected it is returned in a binary order, This is basic computer science.
      Code:
      1
      10
      11
      12
      2
      3 etc
      PHP has a natural order function that you can use
      Code:
      bool natsort ( array &array )
      This function implements a sort algorithm that orders alphanumeric strings 
      in the way a human being would while maintaining key/value associations. 
      This is described as a "natural ordering".

      Comment

      • peeHpee
        New Member
        • Feb 2008
        • 5

        #4
        Sorry, yes by the numbering I was referring to was by the primary key(column field_id2). The problem was solved by switching the data type for the field to integer and then everything feel into place.

        Comment

        Working...