开发者

php manual increment membership no

开发者 https://www.devze.com 2023-02-27 06:22 出处:网络
i\'ve got hundreds of people joining my website and create an membership id for them. i just created a new column in the database called user_no.

i've got hundreds of people joining my website and create an membership id for them. i just created a new column in the database called user_no.

  • whats the mysql query for incrementing the membership no.
  • is it possible to start with AE then numbers ie: AE0001, AE0002, ..开发者_运维问答.. and it starts with 4 number not AE1, AE2..

mysql query:

UPDATE user SET user_no=..??

and on PHP side, how do i increment it? if there is a new member join in.

$db->query("INSERT INTO user (user_no) VALUES(AE'$user_no')");


Why not use an autoincrement field and append AE to it? Autoincrement will be carried out by MySQL so you don't have to worry about it in PHP : http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

And to display your user key in the format of AE0001 you can do

$id = 'AE' . str_pad($autoincrementid, 5, "0", STR_PAD_LEFT);

http://php.net/manual/en/function.str-pad.php


I agree with everyone else who is saying that you should simply use the MySQL Auto-increment feature. That's what it's there for.

It is possible to write your own, possibly using MySQL's MAX() function to find the highest value of a field currently in the table. However unless you're using some very robust transactional code, there is always the danger that this method will result in duplicate records being created when two users create accounts at exactly the same time.

The amount of code required to avoid this is not small, and if you're inexperienced enough not to see the benefits of using Auto-increment then you're unlikely to get it right.

The whole point of Auto-increment is to save you from having to implement all that code every time.

In addition, it is highly recommended for performance reasons to use an integer value for your primary key. Sure, you can display it as "AE" . $id, but you should store it as an integer on the database.

0

精彩评论

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