manels1111 Posted June 11, 2009 Share Posted June 11, 2009 Hey guys another quick question I'm using php to create 15 rows in my database using this code mysql_query("INSERT INTO category (ID, cat_id, category) VALUES ('$ID','1', 'Blank'), ('$ID','2', 'Blank'), ('$ID','3', 'Blank'), ('$ID','4', 'Blank'), ('$ID','5', 'Blank'), ('$ID','6', 'Blank'), ('$ID','7', 'Blank'), ('$ID','8', 'Blank'), ('$ID','9', 'Blank'), ('$ID','10', 'Blank'), ('$ID','11', 'Blank'), ('$ID','12', 'Blank'), ('$ID','13', 'Blank'), ('$ID','14', 'Blank'), ('$ID','15', 'Blank')") or die(mysql_error()); This code works fine except for some reason in my database its listed out of order. In what I'm trying to accomplish I need to input and into my database in the order I layed out here. Instead I got this in the table ID cat_id category 7 15 blank 7 14 blank 7 13 blank 7 12 blank 7 11 blank 7 10 blank 7 9 blank 7 8 blank 7 7 blank 7 5 blank 7 4 blank 7 3 blank 7 2 blank 7 1 blank 7 6 blank I really need to to display in order by the cat_id because when I loop through the array to create text boxes that users will use to update the database it has to be in order without a lot of work on my end. I hope this makes sense. Does anyone know why its not getting written to the database in the order it should be received from my php code. Thanks in advance, Quote Link to comment https://forums.phpfreaks.com/topic/161880-database-row-order/ Share on other sites More sharing options...
J.Daniels Posted June 11, 2009 Share Posted June 11, 2009 I'm not sure why the order is the way it is, but relational databases are based on sets, which do not have order. When you select the data from the database, you specify the order: SELECT * FROM category ORDER BY cat_id Quote Link to comment https://forums.phpfreaks.com/topic/161880-database-row-order/#findComment-854099 Share on other sites More sharing options...
Ken2k7 Posted June 12, 2009 Share Posted June 12, 2009 I hope cat_id is an int as its data type in the DB table. Quote Link to comment https://forums.phpfreaks.com/topic/161880-database-row-order/#findComment-854220 Share on other sites More sharing options...
J.Daniels Posted June 12, 2009 Share Posted June 12, 2009 Ken is correct because if it is of type char it would order it as: 1 10 2 3 4 5 6 7 8 9 The field should be of type int, but if it isn't you can try to cast type the field SELECT * FROM category ORDER BY CAST(cat_id as SIGNED); Quote Link to comment https://forums.phpfreaks.com/topic/161880-database-row-order/#findComment-854227 Share on other sites More sharing options...
manels1111 Posted June 12, 2009 Author Share Posted June 12, 2009 ID is mediumint(9) cat_id is int(2) category is varchar(25) I will try that J. Daniels thanks. Quote Link to comment https://forums.phpfreaks.com/topic/161880-database-row-order/#findComment-854419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.