Avatar of PeterdeB
PeterdeB
Flag for Netherlands asked on

Php sorting mysql results based on id's that have different numbers of digits

Hi

I have a database that needs to be displayed based on their  id, in phpmyadmin it works correctly

1
23
156
276
1245

but in php it sorts like

1
1245
156
23
276


I have tried to search for this here but did not solve it, hope anyone can help me, the id's are all digits

could or should I just insert digits into the numbers that are:
1
23
156

like they will become

0001
0023
0156

if so, how should i do that?

I prefer filling up the id's with more digits, the numbers will never become greater than 5 digits but if for display php can do this so i do not have to alter the database that is even safer i think
PHPMySQL Server

Avatar of undefined
Last Comment
PeterdeB

8/22/2022 - Mon
Ray Paseur

SELECT the column and add 0 to it (or maybe multiply it by 1).  Best wishes, ~Ray
SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PeterdeB

ASKER
it orders correctly in mysql as i have stated, it is defined as int, i have no idea what you mean by your answer with all respect, i did not register here because of my genius in mysql haha anyway thanks ill try
Ray Paseur

OK, perhaps I had it backwards!  To order this information in PHP try natsort().
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
mcnute

In your php, try to form your query like that:

SELECT * FROM yourtable ORDER BY id DESC

Open in new window


That should do it. I think that's what phpmyadmin is doing also. Just try and let me know what happened. But the experts are right to demand your actual php code to help you better.
PeterdeB

ASKER
hey sorry for the late response while being busy with this project my system does suddenly not execute anything anymore except for a few firefox windows
PeterdeB

ASKER
 printf( "%05d", $rows['products_id'] 

Open in new window


using sprintf solved all the issues, thanks all for participating, to your opinion, what should i do with the points, because some of you did actually point me in that direction ofcourse
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
arnold

Choose the answers that helped you and assign points as you see it.
PeterdeB

ASKER
Thanks heaps