1--TEST--
2mysql_affected_rows()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10include_once("connect.inc");
11include_once('setupdefault.inc');
12
13$tmp    = NULL;
14$link   = NULL;
15
16if (0 !== ($tmp = @mysql_affected_rows()))
17	printf("[001] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
18
19if (null !== ($tmp = @mysql_affected_rows($link)))
20	printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22if (!is_null($tmp = @mysql_affected_rows($link, $link)))
23	printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
24
25if (!$link = my_mysql_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
29if (-1 !== ($tmp = mysql_affected_rows($link)))
30	printf("[005] Expecting int/-1, got %s/%s. [%d] %s\n",
31		gettype($tmp), $tmp, mysql_errno($link), mysql_error($link));
32
33if (!mysql_query('DROP TABLE IF EXISTS test', $link))
34	printf("[006] [%d] %s\n", mysql_errno($link), mysql_error($link));
35
36if (!mysql_query('CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine, $link))
37	printf("[007] [%d] %s\n", mysql_errno($link), mysql_error($link));
38
39if (!mysql_query("INSERT INTO test(id, label) VALUES (1, 'a')", $link))
40	printf("[008] [%d] %s\n",  mysql_errno($link), mysql_error($link));
41
42if (1 !== ($tmp = mysql_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@mysql_query("INSERT INTO test(id, label) VALUES (1, 'a')", $link);
47if (-1 !== ($tmp = mysql_affected_rows($link)))
48	printf("[011] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
49
50if (!mysql_query("INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4", $link))
51	printf("[012] [%d] %s\n",  mysql_errno($link), mysql_error($link));
52
53if (2 !== ($tmp = mysql_affected_rows($link)))
54	printf("[013] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
55
56if (!mysql_query("INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')", $link))
57	printf("[014] [%d] %s\n",  mysql_errno($link), mysql_error($link));
58
59if (2 !== ($tmp = mysql_affected_rows($link)))
60	printf("[015] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
61
62if (!mysql_query("INSERT IGNORE INTO test(id, label) VALUES (1, 'a')", $link)) {
63	printf("[016] [%d] %s\n",  mysql_errno($link), mysql_error($link));
64}
65
66if (1 !== ($tmp = mysql_affected_rows($link)))
67	printf("[017] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
68
69if (!mysql_query("INSERT INTO test(id, label) SELECT id + 10, label FROM test", $link))
70	printf("[018] [%d] %s\n",  mysql_errno($link), mysql_error($link));
71
72if (4 !== ($tmp = mysql_affected_rows($link)))
73	printf("[019] Expecting int/4, got %s/%s\n", gettype($tmp), $tmp);
74
75if (!mysql_query("REPLACE INTO test(id, label) values (4, 'd')", $link))
76	printf("[020] [%d] %s\n",  mysql_errno($link), mysql_error($link));
77
78if (2 !== ($tmp = mysql_affected_rows($link)))
79	printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
80
81if (!mysql_query("REPLACE INTO test(id, label) values (5, 'e')", $link))
82	printf("[022] [%d] %s\n",  mysql_errno($link), mysql_error($link));
83
84if (1 !== ($tmp = mysql_affected_rows($link)))
85	printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
86
87if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 2", $link))
88	printf("[024] [%d] %s\n",  mysql_errno($link), mysql_error($link));
89
90if (1 !== ($tmp = mysql_affected_rows($link)))
91	printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
92
93if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 2", $link)) {
94	printf("[025] [%d] %s\n",  mysql_errno($link), mysql_error($link));
95}
96
97if (0 !== ($tmp = mysql_affected_rows($link)))
98	printf("[026] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
99
100if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 100", $link)) {
101	printf("[025] [%d] %s\n",  mysql_errno($link), mysql_error($link));
102}
103
104if (0 !== ($tmp = mysql_affected_rows($link)))
105	printf("[026] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
106
107if (0 !== ($tmp = mysql_affected_rows()))
108	printf("[027] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
109
110if (!mysql_query('DROP TABLE IF EXISTS test', $link))
111	printf("[028] [%d] %s\n", mysql_errno($link), mysql_error($link));
112
113mysql_close($link);
114
115if (false !== ($tmp = @mysql_affected_rows($link)))
116	printf("[029] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
117
118print "done!";
119?>
120--CLEAN--
121<?php
122require_once("clean_table.inc");
123?>
124--EXPECTF--
125done!
126