Results 1 to 3 of 3
  1. #1

    Question PHP insert in random order??

    Hi guys,
    I got a question about php inserting data into mysql DB. I don't understand the reason why everytime when I add some record to my mysql db table, sometimes it add it to the beginning of the table and sometimes it adds it to the end of table. I have already set the primary key and auto_increment to a table field "id". When php inserts the data into mysql, the row with id field 15 sometimes goes before the row with id field 14 (and sometimes vice versa). How can I fix this problem? Is there another way to go around with this? This is very important to me as I want to add latest news to my site, but this is just not working.

    ViscoLink Hosting
    24x7 Support
    30 Days Money Back Guarantee

  2. #2
    Join Date
    Nov 2001
    This isn't a "problem" - the database is performing as it should.

    If you want an ordered result from MySql, use ORDER BY in your query, i.e.

    SELECT * FROM article a ORDER BY desc;

    Creating an identity column or setting a primary "key" doesn't imply sort order, you need to specify how you want your query results ordered...
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  3. #3
    As mwatkins said, database is performing as it's supposed to. Databases were invented for this purpose - to display data in any order you like (one of the uses of course). It's just that default order of data within your table doesn't suit your needs. That means you need to work on your SQL query in order to retrieve data in the order you want to, which is a trivial task. If you need help on that, feel free to post here.

Posting Permissions

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