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