1--TEST--
2mysql_insert_id()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10include "connect.inc";
11include 'setupdefault.inc';
12
13$tmp    = NULL;
14$link   = NULL;
15
16if (0 !== ($tmp = @mysql_insert_id()))
17	printf("[001] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
18
19if (NULL !== ($tmp = @mysql_insert_id($link)))
20	printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22if (!is_null($tmp = @mysql_insert_id($link, 'too many args')))
23	printf("[002a] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
24
25require('table.inc');
26
27if (0 !== ($tmp = mysql_insert_id($link)))
28	printf("[003] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
29
30if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 1", $link)) {
31	printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link));
32}
33if (0 !== ($tmp = mysql_insert_id($link)))
34	printf("[005] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
35mysql_free_result($res);
36
37// no auto_increment column
38if (!$res = mysql_query("INSERT INTO test(id, label) VALUES (100, 'a')", $link)) {
39	printf("[006] [%d] %s\n", mysql_errno($link), mysql_error($link));
40}
41if (0 !== ($tmp = mysql_insert_id($link)))
42	printf("[007] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
43
44if (!$res = mysql_query("ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT", $link)) {
45	printf("[008] [%d] %s\n", mysql_errno($link), mysql_error($link));
46}
47if (!$res = mysql_query("INSERT INTO test(label) VALUES ('a')", $link)) {
48	printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link));
49}
50if (0 === ($tmp = mysql_insert_id($link)))
51	printf("[010] Expecting int/not zero, got %s/%s\n", gettype($tmp), $tmp);
52
53if ($tmp !== ($tmp2 = mysql_insert_id()))
54	printf("[011] Expecting %s/%s, got %s/%s\n",
55		gettype($tmp), $tmp,
56		gettype($tmp2), $tmp2);
57
58mysql_close($link);
59
60var_dump(mysql_insert_id($link));
61
62print "done!";
63?>
64--CLEAN--
65<?php
66require_once("clean_table.inc");
67?>
68--EXPECTF--
69Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
70
71Warning: mysql_insert_id(): %d is not a valid MySQL-Link resource in %s on line %d
72bool(false)
73done!
74