|
Posted by Sanders Kaufman on 08/03/07 11:52
Gordon Burditt wrote:
>>Say, I have a user_insert.php page, and each user accessing this page
>>will insert a foo bar(say, both are user specified strings) a row, and
>>he/she get an ID back---and he/she need to know exactly which ID his/
>>her string corresponds to.
>
> If you *want* interference between users, you'll need to do something
> like "SELECT max(id) FROM ... "
Actually, I find myself having to build stuff in MySQL that *might* end
up on other DBMS's, so relying on auto-unique stuff in MySQL isn't feasible.
To keep my apps from breaking when they migrate like that, I've been
using a UID function. There are a lot of them out there if you query a
search on "UUID +PHP".
By generating a UUID to use as your primary key, instead of the
auto-increment thingy, YOU create the PK value as a "Universally Unique
ID". (Now - I haven't travelled the universe, but my *faith* tells me
it's a safe bet that I won't ever get a duplicate with one of these UUID
functions.)
This has the *added* benefit of avoiding having to do TWO conversations
with the DBMS - one to insert, and one to get the insert ID. Instead,
you just insert it with your own UUID as the PK. One shot, and the
programmer is in control, not the DBMS.
This becomes even *more* efficient if your DBMS and your HTTPd are not
on the same server.
function fnUUID(){
//Returns format: [12345678-1234-1234-123456789012]
$sRetVal = "";
$sRetVal .= sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ) );
return $sRetVal;
}
Navigation:
[Reply to this message]
|