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    $mysqli = new mysqli();
14    if (false !== ($tmp = @$mysqli->affected_rows))
15        printf("[000a] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
16
17    if (!$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket)) {
18        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
19            $host, $user, $db, $port, $socket);
20    }
21
22    if (0 !== ($tmp = $mysqli->affected_rows))
23    printf("[002] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
24
25    if (!$mysqli->query('DROP TABLE IF EXISTS test'))
26        printf("[003] [%d] %s\n", $mysqli->errno, $mysqli->error);
27
28    if (!$mysqli->query('CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine))
29        printf("[004] [%d] %s\n", $mysqli->errno, $mysqli->error);
30
31    if (!$mysqli->query("INSERT INTO test(id, label) VALUES (1, 'a')"))
32        printf("[005] [%d] %s\n",  $mysqli->errno, $mysqli->error);
33
34    if (1 !== ($tmp = $mysqli->affected_rows))
35        printf("[006] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
36
37    // ignore INSERT error, NOTE: command line returns 0, affected_rows returns -1 as documented
38    $mysqli->query("INSERT INTO test(id, label) VALUES (1, 'a')");
39    if (-1 !== ($tmp = $mysqli->affected_rows))
40        printf("[007] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
41
42    if (!$mysqli->query("INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4"))
43        printf("[008] [%d] %s\n",  $mysqli->errno, $mysqli->error);
44
45    if (2 !== ($tmp = $mysqli->affected_rows))
46        printf("[009] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
47
48    if (!$mysqli->query("INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')"))
49        printf("[010] [%d] %s\n",  $mysqli->errno, $mysqli->error);
50
51    if (2 !== ($tmp = $mysqli->affected_rows))
52        printf("[011] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
53
54    if (!$mysqli->query("INSERT IGNORE INTO test(id, label) VALUES (1, 'a')")) {
55        printf("[012] [%d] %s\n",  $mysqli->errno, $mysqli->error);
56    }
57
58    if (1 !== ($tmp = $mysqli->affected_rows))
59        printf("[013] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
60
61    if (!$mysqli->query("INSERT INTO test(id, label) SELECT id + 10, label FROM test"))
62        printf("[014] [%d] %s\n",  $mysqli->errno, $mysqli->error);
63
64    if (4 !== ($tmp = $mysqli->affected_rows))
65        printf("[015] Expecting int/4, got %s/%s\n", gettype($tmp), $tmp);
66
67    if (!$mysqli->query("REPLACE INTO test(id, label) values (4, 'd')"))
68        printf("[015] [%d] %s\n",  $mysqli->errno, $mysqli->error);
69
70    if (2 !== ($tmp = $mysqli->affected_rows))
71        printf("[016] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
72
73    if (!$mysqli->query("REPLACE INTO test(id, label) values (5, 'e')"))
74        printf("[017] [%d] %s\n",  $mysqli->errno, $mysqli->error);
75
76    if (1 !== ($tmp = $mysqli->affected_rows))
77        printf("[018] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
78
79    if (!$mysqli->query("UPDATE test SET label = 'a' WHERE id = 2"))
80        printf("[019] [%d] %s\n",  $mysqli->errno, $mysqli->error);
81
82    if (1 !== ($tmp = $mysqli->affected_rows))
83        printf("[020] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
84
85    if (!$mysqli->query("UPDATE test SET label = 'a' WHERE id = 2")) {
86        printf("[021] [%d] %s\n",  $mysqli->errno, $mysqli->error);
87    }
88
89    if (0 !== ($tmp = $mysqli->affected_rows))
90        printf("[022] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
91
92    if (!$mysqli->query("UPDATE test SET label = 'a' WHERE id = 100")) {
93        printf("[023] [%d] %s\n",  $mysqli->errno, $mysqli->error);
94    }
95
96    if (0 !== ($tmp = $mysqli->affected_rows))
97        printf("[024] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
98
99    if (!$mysqli->query('DROP TABLE IF EXISTS test'))
100        printf("[025] [%d] %s\n", $mysqli->errno, $mysqli->error);
101
102    $mysqli->close();
103
104    if (false !== ($tmp = @$mysqli->affected_rows))
105        printf("[026] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
106
107    print "done!";
108?>
109--CLEAN--
110<?php
111    require_once("clean_table.inc");
112?>
113--EXPECT--
114done!
115