note 95584 deleted from function.mysql-insert-id by googleguy
| From: | googleguy@php.net | Date: | Wed, 19 Jun 2013 22:27:05 +0000 |
| Subject: | note 95584 deleted from function.mysql-insert-id by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195291@lists.php.net to get a copy of this message | ||
Note Submitter: dave
----
There is a lot of incorrect info here on "don't use AI" and "using max is
equivalent". Then you have people improperly advising you to use psuedo-random numbers
If you're really worried about the AI field not returning because of the inherent race
conditions, just do a select statement based on the vars you just input. If your vars are not
unique, DON'T use these psuedo-random numbers. When you have enough iterations, the
probability that the one of these randoms becomes a duplicate gets pretty high.
Instead, just use the unix timestamp. However, don't use UNIX_TIMESTAMP() in your query,
because if you do, when you run your select statement right after, there's a possibility that
you'll end up with a different timestamp.
Since date() continues to count up during the execution of the script, simply store date(U) into a
variable or definition. Then insert and select based on that. Assuming you're not using mysql
procedures:
<?php
define(UNIX_TIMESTAMP, date('U'));
$db->query("insert into table values('', 'a', 'b',
'c', 'd', '".UNIX_TIMESTAMP."'");
$res = $db->query("select id from table where a_col = 'a' and b_col =
'b' and c_col = 'c' and d_col = 'd' and temp_id =
'UNIX_TIMESTAMP'");
...
?>