1--TEST--
2mysqli_insert_id()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12
13    $tmp    = NULL;
14    $link   = NULL;
15
16    if (!is_null($tmp = @mysqli_insert_id()))
17        printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19    if (!is_null($tmp = @mysqli_insert_id($link)))
20        printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22    require('table.inc');
23
24    if (0 !== ($tmp = mysqli_insert_id($link)))
25        printf("[003] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
26
27    if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1")) {
28        printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
29    }
30    if (0 !== ($tmp = mysqli_insert_id($link)))
31        printf("[005] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
32    mysqli_free_result($res);
33
34    // no auto_increment column
35    if (!$res = mysqli_query($link, "INSERT INTO test(id, label) VALUES (100, 'a')")) {
36        printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
37    }
38    if (0 !== ($tmp = mysqli_insert_id($link)))
39        printf("[007] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
40
41    if (!$res = mysqli_query($link, "ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT")) {
42        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
43    }
44
45    if (!$res = mysqli_query($link, "INSERT INTO test(label) VALUES ('a')")) {
46        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
47    }
48    if (($last_id = mysqli_insert_id($link)) <= 0)
49        printf("[010] Expecting int/any >0, got %s/%s\n", gettype($last_id), $last_id);
50
51    if (mysqli_query($link, "LOCK TABLE test WRITE")) {
52        /* we need exclusive access for a moment */
53        /* let's hope nobody changes auto_increment_increment while this code executes */
54        do {
55            if (mysqli_get_server_version($link) >= 50000) {
56                if (!$res = mysqli_query($link, 'SELECT @@auto_increment_increment AS inc')) {
57                    printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58                    break;
59                }
60                if (!$row = mysqli_fetch_assoc($res)) {
61                    printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
62                    break;
63                }
64                mysqli_free_result($res);
65                $inc = $row['inc'];
66            } else {
67                $inc = 1;
68            }
69
70            if (!mysqli_query($link, "INSERT INTO test(label) VALUES ('b')")) {
71                printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
72                break;
73            }
74            if (($next_id = mysqli_insert_id($link)) <= $last_id)
75                /*
76                very likely a bug, but someone could have done something on the server
77                between the second last insert and the lock, therefore don't stop just bail
78                */
79                printf("[014] Expecting int/any > %d, got %s/%s\n", $last_id, gettype($next_id), $next_id);
80
81            $last_id = $next_id;
82            if (!mysqli_query($link, "INSERT INTO test(label) VALUES ('c'), ('d'), ('e')")) {
83                printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
84                break;
85            }
86            /*
87            Note: For a multiple-row insert, LAST_INSERT_ID() and mysql_insert_id() actually
88            return the AUTO_INCREMENT key from the first of the inserted rows. This allows
89            multiple-row inserts to be reproduced correctly on other servers in a replication setup.
90            */
91            if (($next_id = mysqli_insert_id($link)) != $last_id + $inc) {
92                printf("[016] Expecting int/%d, got %s/%s\n", $last_id + 1, gettype($next_id), $next_id);
93                break;
94            }
95
96            if (!$res = mysqli_query($link, "SELECT LAST_INSERT_ID() AS last_id")) {
97                printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
98                break;
99            }
100            if (!$row = mysqli_fetch_assoc($res)) {
101                printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
102                break;
103            }
104            mysqli_free_result($res);
105
106            if ($next_id != $row['last_id']) {
107                printf("[019] Something is wrong, check manually. Expecting %s got %s.\n",
108                    $next_id, $row['last_id']);
109                break;
110            }
111        } while (false);
112        mysqli_query($link, "UNLOCK TABLE test");
113    }
114
115    if (!$res = mysqli_query($link, "INSERT INTO test(id, label) VALUES (1000, 'a')")) {
116        printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
117    }
118    if (1000 !== ($tmp = mysqli_insert_id($link)))
119        printf("[021] Expecting int/1000, got %s/%s\n", gettype($tmp), $tmp);
120
121    if (!$res = mysqli_query($link, "INSERT INTO test(label) VALUES ('b'), ('c')")) {
122        printf("[022] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
123    }
124    if (1000 >= ($tmp = mysqli_insert_id($link)))
125        printf("[023] Expecting int/>1000, got %s/%s\n", gettype($tmp), $tmp);
126
127    mysqli_close($link);
128
129    var_dump(mysqli_insert_id($link));
130
131    print "done!";
132?>
133--CLEAN--
134<?php
135    require_once("clean_table.inc");
136?>
137--EXPECTF--
138Warning: mysqli_insert_id(): Couldn't fetch mysqli in %s on line %d
139bool(false)
140done!
141