summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/bug79375.phpt
blob: 6c6176311daa3794f178aafb97bf45148b03f485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
--TEST--
Bug #79375: mysqli_store_result does not report error from lock wait timeout
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifconnectfailure.inc');
if (!defined('MYSQLI_STORE_RESULT_COPY_DATA')) die('skip requires mysqlnd');
?>
--FILE--
<?php

require_once("connect.inc");
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
$mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);

$mysqli->query('DROP TABLE IF EXISTS test');
$mysqli->query('CREATE TABLE test (first int) ENGINE = InnoDB');
$mysqli->query('INSERT INTO test VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)');

function testStmtStoreResult(mysqli $mysqli, string $name) {
    $mysqli->query("SET innodb_lock_wait_timeout = 1");
    $mysqli->query("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    $stmt = $mysqli->prepare($query);
    $stmt->execute();
    try {
        $stmt->store_result();
        echo "Got {$stmt->num_rows} for $name\n";
    } catch(mysqli_sql_exception $e) {
        echo $e->getMessage()."\n";
    }
}
function testStmtGetResult(mysqli $mysqli, string $name) {
    $mysqli->query("SET innodb_lock_wait_timeout = 1");
    $mysqli->query("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    $stmt = $mysqli->prepare($query);
    $stmt->execute();
    try {
        $res = $stmt->get_result();
        echo "Got {$res->num_rows} for $name\n";
    } catch(mysqli_sql_exception $e) {
        echo $e->getMessage()."\n";
    }
}
function testNormalQuery(mysqli $mysqli, string $name) {
    $mysqli->query("SET innodb_lock_wait_timeout = 1");
    $mysqli->query("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    try {
        $res = $mysqli->query($query);
        echo "Got {$res->num_rows} for $name\n";
    } catch(mysqli_sql_exception $e) {
        echo $e->getMessage()."\n";
    }
}
function testStmtUseResult(mysqli $mysqli, string $name) {
    $mysqli->query("SET innodb_lock_wait_timeout = 1");
    $mysqli->query("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    $stmt = $mysqli->prepare($query);
    $stmt->execute();
    try {
        $stmt->fetch(); // should throw an error
        $stmt->fetch();
        echo "Got {$stmt->num_rows} for $name\n";
    } catch (mysqli_sql_exception $e) {
        echo $e->getMessage()."\n";
    }
}
function testResultFetchRow(mysqli $mysqli, string $name) {
    $mysqli->query("SET innodb_lock_wait_timeout = 1");
    $mysqli->query("START TRANSACTION");
    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
    echo "Running query on $name\n";
    $res = $mysqli->query($query, MYSQLI_USE_RESULT);
    try {
        $res->fetch_row();
        $res->fetch_row();
        echo "Got {$res->num_rows} for $name\n";
    } catch(mysqli_sql_exception $e) {
        echo $e->getMessage()."\n";
    }
}

testStmtStoreResult($mysqli, 'first connection');
testStmtStoreResult($mysqli2, 'second connection');

$mysqli->close();
$mysqli2->close();

echo "\n";
//  try it again for get_result
$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
$mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);

testStmtGetResult($mysqli, 'first connection');
testStmtGetResult($mysqli2, 'second connection');

$mysqli->close();
$mysqli2->close();

echo "\n";
//  try it again with unprepared query
$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
$mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);

testNormalQuery($mysqli, 'first connection');
testNormalQuery($mysqli2, 'second connection');

$mysqli->close();
$mysqli2->close();

echo "\n";
//  try it again with unprepared query
$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
$mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);

testStmtUseResult($mysqli, 'first connection');
testStmtUseResult($mysqli2, 'second connection');

$mysqli->close();
$mysqli2->close();

echo "\n";
//  try it again using fetch_row on a result object
$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
$mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);

testResultFetchRow($mysqli, 'first connection');
testResultFetchRow($mysqli2, 'second connection');

$mysqli->close();
$mysqli2->close();

?>
--CLEAN--
<?php
    require_once("clean_table.inc");
?>
--EXPECTF--
Running query on first connection
Got %d for first connection
Running query on second connection
Lock wait timeout exceeded; try restarting transaction

Running query on first connection
Got %d for first connection
Running query on second connection
Lock wait timeout exceeded; try restarting transaction

Running query on first connection
Got %d for first connection
Running query on second connection
Lock wait timeout exceeded; try restarting transaction

Running query on first connection
Got %d for first connection
Running query on second connection
Lock wait timeout exceeded; try restarting transaction

Running query on first connection
Got 1 for first connection
Running query on second connection

Warning: mysqli_result::fetch_row(): Error while reading a row in %s on line %d
Got 0 for second connection