473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. $sql = "select * from infop2 order by field_id2";
  2. $result = mysql_query($sql,$connection);
  3. $rows = mysql_num_rows($result);
  4. for ($r = 1; $r <= $rows; $r++) {
  5. $row = mysql_fetch_array($result);
  6. 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.
Feb 22 '08 #1
3 1004
OK fixed it. Moderators please erase this if you like.
Feb 22 '08 #2
code green
1,726 Expert 1GB
By row I take that you mean field_id2.
The actual database table row numbers are meaningless
OK you have ordered by field_id2
Expand|Select|Wrap|Line Numbers
  1. select * from infop2 order by field_id2
And as expected it is returned in a binary order, This is basic computer science.
Expand|Select|Wrap|Line Numbers
  1. 1
  2. 10
  3. 11
  4. 12
  5. 2
  6. 3 etc
PHP has a natural order function that you can use
Expand|Select|Wrap|Line Numbers
  1. bool natsort ( array &array )
  2. This function implements a sort algorithm that orders alphanumeric strings 
  3. in the way a human being would while maintaining key/value associations. 
  4. This is described as a "natural ordering".
Feb 22 '08 #3
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.
Feb 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Matt Fletcher | last post by:
Hi guys, I am trying to allow the models in a mysql database to be ordered by the site owner. I was thinking along the lines of a <SELECT> list containing the model names and Up and Down...
2
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need...
2
by: masood.iqbal | last post by:
What is the standard C/C++ lexicograhic ordering of punctuation characters with respect to each other and the alphanumeric characters? --Masood
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
5
by: deane.barker | last post by:
Consider this SQL: SELECT my_field FROM my_table WHERE my_field IN ('value2', 'value1', 'value3') Simple enough, but is there anyway to specify that the result should be ordered exactly like...
0
by: Luongo | last post by:
Hi, I'm working on a mysql-based survey program. On the user page I display the survey form, and on the admin page I display the results for each question. The problem, which I find puzzling, is...
10
by: bill | last post by:
I have a database of kennels. One page lists the kennels alphabetically by kennel name. I have a field for kennel name and an index with kennel name as the only field. Some of the kennels have...
5
by: lilkev | last post by:
Hey, I'm new to MySQL and web development as a whole. I designed and put up a website at ArtistsVale , effectively the bulk of the site is an Image gallery which I created through...
4
ChrisWang
by: ChrisWang | last post by:
Dear all, I am reading the book "Core Python Programming". In the chapter talking about modules, it says the modules should follow this ordering: import Python Standard Library modules ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.