1--TEST--
2mysqli_stmt_insert_id()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10    require_once("connect.inc");
11
12    require('table.inc');
13
14    $stmt = mysqli_stmt_init($link);
15
16    try {
17        mysqli_stmt_insert_id($stmt);
18    } catch (Error $exception) {
19        echo $exception->getMessage() . "\n";
20    }
21
22    if (!mysqli_stmt_prepare($stmt, "SELECT id, label FROM test ORDER BY id LIMIT 1") ||
23        !mysqli_stmt_execute($stmt)) {
24        printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
25    }
26
27    if (0 !== ($tmp = mysqli_stmt_insert_id($stmt)))
28        printf("[005] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
29    mysqli_stmt_close($stmt);
30
31    // no auto_increment column
32    $stmt = mysqli_stmt_init($link);
33    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'a')") ||
34        !mysqli_stmt_execute($stmt)) {
35        printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
36    }
37
38    if (0 !== ($tmp = mysqli_stmt_insert_id($stmt)))
39        printf("[007] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
40
41    if (mysqli_get_server_version($link) > 50000 &&
42        (!mysqli_stmt_prepare($stmt, "ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT") ||
43        !mysqli_stmt_execute($stmt))) {
44        printf("[008] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
45    } else if (mysqli_get_server_version($link) < 50000){
46        mysqli_query($link, "ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT");
47    }
48
49    if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(label) VALUES ('a')") ||
50        !mysqli_stmt_execute($stmt)) {
51        printf("[009] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
52    }
53    if (0 === ($tmp = mysqli_stmt_insert_id($stmt)))
54        printf("[010] Expecting int/any non zero, got %s/%s\n", gettype($tmp), $tmp);
55    mysqli_stmt_close($stmt);
56
57    mysqli_close($link);
58
59    try {
60        mysqli_stmt_insert_id($stmt);
61    } catch (Error $exception) {
62        echo $exception->getMessage() . "\n";
63    }
64
65    print "done!";
66?>
67--CLEAN--
68<?php
69    require_once("clean_table.inc");
70?>
71--EXPECT--
72mysqli_stmt object is not fully initialized
73mysqli_stmt object is already closed
74done!
75