1--TEST--
2mysqli_stmt_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_stmt_insert_id()))
17        printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19    $stmt = @new mysqli_stmt($link);
20    if (!is_null($tmp = @mysqli_insert_id($link)))
21        printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
22
23    require('table.inc');
24
25    $stmt = mysqli_stmt_init($link);
26    if (false !== ($tmp = @mysqli_stmt_insert_id($stmt)))
27        printf("[003] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
28
29    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 1") ||
30        !mysqli_stmt_execute($stmt)) {
31        printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
32    }
33
34    if (0 !== ($tmp = mysqli_stmt_insert_id($stmt)))
35        printf("[005] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
36    mysqli_stmt_close($stmt);
37
38    // no auto_increment column
39    $stmt = mysqli_stmt_init($link);
40    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'a')") ||
41        !mysqli_stmt_execute($stmt)) {
42        printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
43    }
44
45    if (0 !== ($tmp = mysqli_stmt_insert_id($stmt)))
46        printf("[007] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
47
48    if (mysqli_get_server_version($link) > 50000 &&
49        (!mysqli_stmt_prepare($stmt, "ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT") ||
50        !mysqli_stmt_execute($stmt))) {
51        printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
52    } else if (mysqli_get_server_version($link) < 50000){
53        mysqli_query($link, "ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT");
54    }
55
56    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(label) VALUES ('a')") ||
57        !mysqli_stmt_execute($stmt)) {
58        printf("[009] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
59    }
60    if (0 === ($tmp = mysqli_stmt_insert_id($stmt)))
61        printf("[010] Expecting int/any non zero, got %s/%s\n", gettype($tmp), $tmp);
62    mysqli_stmt_close($stmt);
63
64    mysqli_close($link);
65
66    var_dump(mysqli_stmt_insert_id($stmt));
67
68    print "done!";
69?>
70--CLEAN--
71<?php
72    require_once("clean_table.inc");
73?>
74--EXPECTF--
75Warning: mysqli_stmt_insert_id(): Couldn't fetch mysqli_stmt in %s on line %d
76bool(false)
77done!
78