开发者

How to add 0's infront with auto increment in a mysql database

My problem in fairly simple. I have a login system constructed using php, mysql databases, and ajax. Everything was working until today when I found a very potential problem that could completely confuse the system. Every single user in the database has a id that is automatically generated by setting auto-increment for the id column to give them a unique id. now my problem is that for example, my chat system, is trying to display all the users in the room. Well, the id for my first user is 1, and the id for the second user is 104. So, php extracts all the users id's from the database, which is stored in this format: user1id,user2id,user3id and so on. But when php searches those id's for id #1 to see if it exists, it finds a 1 in id 104. That's where the problem starts. Sensing that the user is already in, php changes the record accordingly, and updates the user list to this: 04,1. It was 104,1 before. Now, the script tries to locate the username for id #04, which doesn't exist. Therefore, a incorrect list of users in the room is given. Now, the most sensible solution to this I think is to add 0's infront of the shorter id's, so instead of 开发者_如何学运维104,1,it's 104,001. But How exactly do I do that with auto increment? Thanks in advance.


When searching for users with a comma deliminated list, try using explode() function to turn the list into an array then doing the search using the in_array() function


You can do what you are asking for by make the id field "zero-filled".

But there are many better approaches to this problem...


Thanks everyone, but I found a solution. I would get the longest value in the array after explode, and use str_pad to add enough 0's to the end of every value in the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜