I want my UserId field to be like this AB001.
Where AB is the name of anything and is constant, and 001 is the auto-increment value.
How can I do that in PHP and MySql.
I want my UserId field to be like this AB001.
Where AB is the name of anything and is constant, and 001 is the auto-increment value.
How can I do that in PHP and MySql.
In PHP you can increment the var like this:
$var = "AB001";
for($i = 0; $i < 10; $i++) {
echo $var . "\n";
$var++;
}
Then you just need to insert it into mysql...
EDIT ----
However you may encounter ID collisions if two processes create the same ID at the same time!
Another Edit ----
You could make a function like this and pass in the DB ID and the base format like this:
$base = "AB000";
echo createId($base, 1456);
function createId($base, $dbId)
{
for ($i = 0; $i < $dbId; $i++) {
$base++;
}
return $base;
}
I hope this is what your looking for :)
Please read following questions:
You could also try to use triggers if you really depend on having this field in the database.
Otherwise build the AB part at run time to show it to the user.
Please look at this Fiddle. Let me know if you need any more clarifications.