xref: /PHP-8.2/ext/pdo_mysql/tests/bug79375.phpt (revision 72f47c0c)
1--TEST--
2Bug #79375: mysqli_store_result does not report error from lock wait timeout
3--EXTENSIONS--
4pdo
5pdo_mysql
6--SKIPIF--
7<?php
8if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
9require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
10MySQLPDOTest::skip();
11?>
12--FILE--
13<?php
14require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
15
16function createDB(): PDO {
17    $db = MySQLPDOTest::factory();
18    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
19    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
20    return $db;
21}
22
23$db = createDB();
24$db2 = createDB();
25$db->query('DROP TABLE IF EXISTS test');
26$db->query('CREATE TABLE test (first int) ENGINE = InnoDB');
27$db->query('INSERT INTO test VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)');
28
29function testNormalQuery(PDO $db, string $name) {
30    $db->exec("SET innodb_lock_wait_timeout = 1");
31    $db->exec("START TRANSACTION");
32    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
33    echo "Running query on $name\n";
34    try {
35        $stmt = $db->query($query);
36        echo "Got {$stmt->rowCount()} for $name\n";
37    } catch (PDOException $e) {
38        echo $e->getMessage()."\n";
39    }
40}
41
42function testPrepareExecute(PDO $db, string $name) {
43    $db->exec("SET innodb_lock_wait_timeout = 1");
44    $db->exec("START TRANSACTION");
45    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
46    echo "Running query on $name\n";
47    $stmt = $db->prepare($query);
48    try {
49        $stmt->execute();
50        echo "Got {$stmt->rowCount()} for $name\n";
51    } catch (PDOException $e) {
52        echo $e->getMessage()."\n";
53    }
54}
55
56function testUnbuffered(PDO $db, string $name) {
57    $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
58    $db->exec("SET innodb_lock_wait_timeout = 1");
59    $db->exec("START TRANSACTION");
60    $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
61    echo "Running query on $name\n";
62    $stmt = $db->prepare($query);
63    $stmt->execute();
64    try {
65        $rows = $stmt->fetchAll();
66        $count = count($rows);
67        echo "Got $count for $name\n";
68    } catch (PDOException $e) {
69        echo $e->getMessage()."\n";
70    }
71}
72
73testNormalQuery($db, 'first connection');
74testNormalQuery($db2, 'second connection');
75unset($db);
76unset($db2);
77echo "\n";
78
79$db = createDB();
80$db2 = createDB();
81testPrepareExecute($db, 'first connection');
82testPrepareExecute($db2, 'second connection');
83unset($db);
84unset($db2);
85echo "\n";
86
87$db = createDB();
88$db2 = createDB();
89testUnbuffered($db, 'first connection');
90testUnbuffered($db2, 'second connection');
91unset($db);
92unset($db2);
93echo "\n";
94
95?>
96--CLEAN--
97<?php
98require __DIR__ . '/mysql_pdo_test.inc';
99MySQLPDOTest::dropTestTable();
100?>
101--EXPECT--
102Running query on first connection
103Got 1 for first connection
104Running query on second connection
105SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
106
107Running query on first connection
108Got 1 for first connection
109Running query on second connection
110SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
111
112Running query on first connection
113Got 1 for first connection
114Running query on second connection
115SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
116