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