Results 1 to 3 of 3

Thread: SQL entries out of order

  1. #1
    Join Date
    Jun 2005

    SQL entries out of order

    I am sending some variables (using a while loop) to an SQL table. For some reason my script is inserting them in the opposite order that they should be.
    include 'config.php'; // contains DB info
    $i = 1; // variable variable starts at 1
    while (!empty(${barcode.$i})) { // the variable $barcode# increments with each loop. it loops as long as #barcode isn't empty.
    	mysql_query("INSERT INTO files (barcode, serial, manufacturer, description, number, home_loc, notes) VALUES ('${barcode.$i}', '${serial.$i}', '${manufacturer.$i}', '${description.$i}', '${number.$i}', '${home_loc.$i}', '${notes.$i}')") or die(mysql_error());
    	echo "Successfully added ".${description.$i}." <br>";
    I would think that $barcode1 would show up first in the table, but it shows up last. The order is exactly opposite of what it should be. I know I could reorder it with more code, but it should work without more code, right?

  2. #2
    Join Date
    Jul 2010
    Using MyISAM:
    ALTER TABLE example ORDER BY columnName [DESC | ASC];

    Try it with InnoDB you get:
    1105 ORDER BY ignored as there is a user-defined clustered index in the table 'example'.

    In the grand scheme of things there is really no order and adding extra trivial comparisons is undesired by people who do not care about order. If you use InnoDB the order will be delivered in PRIMARY KEY order, not INSERT order. Regardless of your engine this is an artifact of the underlying implementation and NOT something to depend on.

    If you want the result set in a certain order, you should add the ORDER BY clause to the query.

    Edit: oh.. hmm, ya this isn't a mysql question.

    The results are showing up in the correct order- it is you who has made the mistake. I don't know what the mistake is. Probably something inside of config.php
    Last edited by eval(BadCode); 06-12-2011 at 05:25 AM.
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  3. #3
    Join Date
    Aug 2004
    Unless I'm misunderstanding what you mean by "wrong order", it should not matter: that's what the "ORDER BY" clause in any queries against that data should be for. (Maybe you need to add an auto-increment integer primary key field to that table, should you for some reason actually need to sort it by insert order?)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Recent Articles