Re: Creating alphanumeric id for a table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You can use mysql's autoincrement ,but you must do some php for
special display,follow is my method.

1.create an int or smallint mysql col.
2.set it autoincrement.
3.insert some dates to database.
4.select dates and display with letter prefix.
for example: 12->AAA0012,AAA1234,
you can make an php functions to change num to prefix letter .
If you want letter autoincrement like int vars create an 26 based
calculate function for mapping number to letters.
ex:
10001->AAB0001
20001->AAC0001

2009/9/11 Phpster <phpster@xxxxxxxxx>:
>
>
>
>
> On Sep 11, 2009, at 3:50 AM, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx>
> wrote:
>
>> On Fri, 2009-09-11 at 00:19 -0400, Phpster wrote:
>>>
>>> Note that this approach has risks around race conditions. Anytime you
>>> have a construct for the id you run the risk of having it create
>>> duplicate ids. You will need to handle that.
>>>
>>> Bastien
>>>
>>> Sent from my iPod
>>>
>>> On Sep 10, 2009, at 6:49 PM, Ben Dunlap <bdunlap@xxxxxxxxxxxxxxxxxx>
>>> wrote:
>>>
>>>>> I assume that I can get increment value/sequence from db  (I used
>>>>> harcoded
>>>>> increment value  in the code above (generate_id(1))),
>>>>> but I don't know how I can get this incremental value from db.I use
>>>>> mysql
>>>>> 5.0.
>>>>
>>>> If you're thinking of retrieving the newest value of an AUTO_INCREMENT
>>>> column, immediately after inserting a row, there are different ways to
>>>> do this depending on how you're connecting to MySQL.
>>>>
>>>> PDO, for example, has a method called lastInsertId():
>>>> http://us2.php.net/manual/en/pdo.lastinsertid.php
>>>>
>>>> And the mysql_* family of functions has mysql_insert_id(), etc.
>>>>
>>>> Ben
>>>>
>>>> --
>>>> PHP General Mailing List (http://www.php.net/)
>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>
>>>
>> Not if you use the functions built specifically for that, i.e. the ones
>> that Ben mentioned. In no circumstances should you ever do a "SELECT
>> MAX(id) FROM table". I've seen this done so many times by people who
>> should know better, and they even saw no problems when they ran tests
>> (that was, they tested the systems themselves, and only themselves) but
>> then it all fell down as soon as multiple users tried to use it.
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>
>
> I totally agree. Ufortunately, my boss would not agree and the one app I
> have to support is full of race conditions. I told them from the beginning
> that it would not work and under any load ( btw the max app load is about 40
> people on a server, don't ask, it would make me sad) we get errors in the
> app. I get to spend a few hours a week fixing this crap!
>
> I did just want to point out that there is risk in not using auto numbers or
> other unique id mechanisms if you don't pay attention.
>>
>
>
> Bastien
>
> Sent from my iPod
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux