· 8 years ago · Aug 10, 2018, 10:24 PM
1mysql_insert_id and last_insert_id wrong behavior
2CREATE TABLE IF NOT EXISTS `t5` (
3 `id` int(11) NOT NULL auto_increment,
4 `a` int(11) NOT NULL,
5 `b` int(11) NOT NULL,
6 PRIMARY KEY (`id`),
7 UNIQUE KEY `a` (`a`,`b`)
8 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
9
10$db = DBFactory::getInstance();
11 $db->selectDB('test');
12 $db->query("insert into t5 (a, b) values(1, 1) on duplicate key update a=1, b=1");
13 var_dump(mysql_insert_id());
14
15 $cur = $db->query("select last_insert_id()");
16 var_dump(mysql_fetch_assoc($cur));
17
18int(1)
19array(1) {
20 ["last_insert_id()"]=>
21 string(1) "1"
22}
23
24int(1)
25array(1) {
26 ["last_insert_id()"]=>
27 string(1) "2"
28}
29
30int(1)
31array(1) {
32 ["last_insert_id()"]=>
33 string(1) "1"
34}
35
36int(2)
37array(1) {
38 ["last_insert_id()"]=>
39 string(1) "2"
40}
41
42DROP TABLE IF EXISTS `t5`;
43CREATE TABLE IF NOT EXISTS `t5` (
44 `id` int(11) NOT NULL auto_increment,
45 `a` int(11) NOT NULL,
46 `b` int(11) NOT NULL,
47 `t` int(11) NOT NULL,
48 PRIMARY KEY (`id`),
49 UNIQUE KEY `a` (`a`,`b`)
50) ENGINE=InnoDB DEFAULT CHARSET=latin1;
51
52$db = DBFactory::getInstance();
53$db->selectDB('test');
54$db->query("insert into t5 (a, b, t) values(1, 1, ".time().") on duplicate key update a=1, b=1, t=".time());
55//$db->query("insert ignore into t5 (a, b) values(1, 1)");
56var_dump(mysql_insert_id());
57
58$cur = $db->query("select last_insert_id()");
59var_dump(mysql_fetch_assoc($cur));
60
61int(1)
62array(1) {
63 ["last_insert_id()"]=>
64 string(1) "2"
65}
66
67int(1)
68array(1) {
69 ["last_insert_id()"]=>
70 string(1) "2"
71}
72
73int(1)
74array(1) {
75 ["last_insert_id()"]=>
76 string(1) "0"
77}