简体   繁体   中英

php-mysql how to increment a binary, UUID primary key and get inserted id

how to autoincrement a binary column .and how to get the mysql_insert_id/last insert id... i am doing like this

$on="INSERT INTO `public_poll`(`poll_id`,`poll_closing_date`,`active`)
VALUES((select UNHEX(REPLACE(UUID(),'-',''))),(select DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 8 DAY)),1)";
 $onj=mysql_query($on);

but how to get the inserted id

Originally, I thought you were working with an AUTO_INCREMENT field.

Now that I notice that you're using a UUID field stored as a binary, I'd recommend that you move to Postgres, instead.

Basically, you don't ever want to "autoincrement" UUIDs, because, that simply produces mangled UUIDs. You see, the theory behind UUID inserts is that you absolutely shouldn't fetch an ID after its insertion; instead, you should have generated the UUID in the original app, and remembered it there -- PHP, in this case.

Personally, on a Linux system, I simply exec() against libuuid (sp?) and use that value all over the place.

There also exists various UUID libraries for PHP, like this thing: http://pecl.php.net/package/uuid

Edit: More info here: Substitute for MySQL's UUID Version 1 function?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM