kreut Posted February 4, 2011 Share Posted February 4, 2011 Hello! I'd like to create a username automatically based on a first name that's entered in a form and then concatenated with the largest user_id that's currently in the table (to ensure a unique username --- the user_id is a primary key). Is there a way using the Zend Framework to access the maximum id? And, what would the concatenation syntax be? Thorough answers would be appreciated as I'm still very much a newbie. Thank you... Quote Link to comment https://forums.phpfreaks.com/topic/226726-largest-id/ Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2011 Share Posted February 4, 2011 Why not just set a UNIQUE index on the username field of the DB, and check to make sure the username is available before trying to insert it? Quote Link to comment https://forums.phpfreaks.com/topic/226726-largest-id/#findComment-1170032 Share on other sites More sharing options...
kreut Posted February 5, 2011 Author Share Posted February 5, 2011 I like your idea and that would work for sure. However, I think that it would be easier for me as an administrator of the system to have it automatically create the username for me. However, if there's no quick solution, then I'll go with your suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/226726-largest-id/#findComment-1170092 Share on other sites More sharing options...
lazylodr Posted February 5, 2011 Share Posted February 5, 2011 While I don't think that what you are trying to accomplish is great, and tend to side Pikachu2000 ... If you really wanted to do this, you could use a "SELECT MAX(user_id) FROM users" to get the highest user id, but that's not necessarily the best thing to do (if you have a high traffic site with two users that sign up at the same time, they could both get the same id. It's not likely, but it could happen) The other thing you could do, if you are using an auto-increment id field on the table, is perform the insert and get the auto-increment value, and then update the username on the record to the first name + id. This is a better solution that using the MAX query, the drawback being an additional update query. If you are using Oracle as your database, this is easy to do with a sequence. Simple query the nextval from the sequence (SELECT user_seq.nextval FROM dual;) and use that value for both your primary key, and the concatenation with the first name when you perform the insert. Quote Link to comment https://forums.phpfreaks.com/topic/226726-largest-id/#findComment-1170434 Share on other sites More sharing options...
kreut Posted February 6, 2011 Author Share Posted February 6, 2011 Thank you both for you input. I really like this idea: "The other thing you could do, if you are using an auto-increment id field on the table, is perform the insert and get the auto-increment value, and then update the username on the record to the first name + id. This is a better solution that using the MAX query, the drawback being an additional update query." and I will implement it. One additional query won't be a big deal. Quote Link to comment https://forums.phpfreaks.com/topic/226726-largest-id/#findComment-1170495 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.