Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/226726-largest-id/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/226726-largest-id/#findComment-1170434
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/226726-largest-id/#findComment-1170495
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.