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