1--TEST--
2mysqli_affected_rows()
3--SKIPIF--
4<?php
5	require_once('skipif.inc');
6	require_once('skipifemb.inc');
7	require_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_affected_rows()))
17		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
18
19	if (!is_null($tmp = @mysqli_affected_rows($link)))
20		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	if (!is_null($tmp = @mysqli_affected_rows($link, $link)))
23		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
24
25	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
26		printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
27			$host, $user, $db, $port, $socket);
28
29	if (0 !== ($tmp = mysqli_affected_rows($link)))
30		printf("[005] Expecting int/0, got %s/%s. [%d] %s\n",
31			gettype($tmp), $tmp, mysqli_errno($link), mysqli_error($link));
32
33	if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
34		printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
35
36	if (!mysqli_query($link, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine))
37		printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
38
39	if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a')"))
40		printf("[008] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
41
42	if (1 !== ($tmp = mysqli_affected_rows($link)))
43		printf("[010] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
44
45	// ignore INSERT error, NOTE: command line returns 0, affected_rows returns -1 as documented
46	mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a')");
47	if (-1 !== ($tmp = mysqli_affected_rows($link)))
48		printf("[011] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
49
50	if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4"))
51		printf("[012] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
52
53	if (2 !== ($tmp = mysqli_affected_rows($link)))
54		printf("[013] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
55
56	if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')"))
57		printf("[014] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
58
59	if (2 !== ($tmp = mysqli_affected_rows($link)))
60		printf("[015] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
61
62	if (!mysqli_query($link, "INSERT IGNORE INTO test(id, label) VALUES (1, 'a')")) {
63		printf("[016] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
64	}
65
66	if (1 !== ($tmp = mysqli_affected_rows($link)))
67		printf("[017] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
68
69	if (!mysqli_query($link, "INSERT INTO test(id, label) SELECT id + 10, label FROM test"))
70		printf("[018] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
71
72	if (4 !== ($tmp = mysqli_affected_rows($link)))
73		printf("[019] Expecting int/4, got %s/%s\n", gettype($tmp), $tmp);
74
75	if (!mysqli_query($link, "REPLACE INTO test(id, label) values (4, 'd')"))
76		printf("[020] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
77
78	if (2 !== ($tmp = mysqli_affected_rows($link)))
79		printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
80
81	if (!mysqli_query($link, "REPLACE INTO test(id, label) values (5, 'e')"))
82		printf("[022] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
83
84	if (1 !== ($tmp = mysqli_affected_rows($link)))
85		printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
86
87	if (!mysqli_query($link, "UPDATE test SET label = 'a' WHERE id = 2"))
88		printf("[024] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
89
90	if (1 !== ($tmp = mysqli_affected_rows($link)))
91		printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
92
93	$charsets = array('utf8');
94	foreach ($charsets as $k => $charset) {
95		if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $charset))))
96			continue;
97		mysqli_free_result($res);
98		if (true !== ($tmp = mysqli_set_charset($link, $charset)))
99			printf("[026] Expecting boolean/true got %s/%s\n",
100				gettype($tmp), $tmp);
101		if (0 !== ($tmp = mysqli_affected_rows($link)))
102			printf("[027] Expecting int/0 got %s/%s\n", gettype($tmp), $tmp);
103	}
104
105	if (!mysqli_query($link, "UPDATE test SET label = 'a' WHERE id = 2")) {
106		printf("[028] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
107	}
108
109	if (0 !== ($tmp = mysqli_affected_rows($link)))
110		printf("[029] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
111
112	if (!mysqli_query($link, "UPDATE test SET label = 'a' WHERE id = 100")) {
113		printf("[030] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
114	}
115
116	if (0 !== ($tmp = mysqli_affected_rows($link)))
117		printf("[031] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
118
119	if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
120		printf("[032] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
121
122
123	mysqli_close($link);
124
125	if (NULL !== ($tmp = @mysqli_affected_rows($link)))
126		printf("[033] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
127
128	print "done!";
129?>
130--CLEAN--
131<?php
132	require_once("clean_table.inc");
133?>
134--EXPECTF--
135done!