Skip to content

Commit 8c2e425

Browse files
authored
MySqliDriver::getResource() fixed usage of resource being closed prior to the call in PHP 8 (#410)
1 parent 26f2657 commit 8c2e425

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

src/Dibi/Drivers/MySqliDriver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Dibi\Drivers;
1111

1212
use Dibi;
13+
use Throwable;
1314

1415

1516
/**
@@ -246,7 +247,11 @@ public function rollback(string $savepoint = null): void
246247
*/
247248
public function getResource(): ?\mysqli
248249
{
249-
return @$this->connection->thread_id ? $this->connection : null;
250+
try {
251+
return @$this->connection->thread_id ? $this->connection : null;
252+
} catch (Throwable $e) {
253+
return null;
254+
}
250255
}
251256

252257

tests/databases.appveyor.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ username = root
2323
password = "Password12!"
2424
system = mysql
2525

26+
[mysqli-driver]
27+
driver = mysqli
28+
host = 127.0.0.1
29+
username = root
30+
password = "Password12!"
31+
charset = utf8
32+
system = mysql
33+
2634
[odbc]
2735
driver = odbc
2836
dsn = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=data/odbc.mdb"

tests/databases.github.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ user = root
4040
password = root
4141
system = mysql
4242

43+
[mysqli-driver]
44+
driver = mysqli
45+
host = "127.0.0.1"
46+
database = dibi_test
47+
username = root
48+
password = root
49+
port = 3307
50+
system = mysql
51+
4352
[postgre 9.6]
4453
driver = postgre
4554
host = "127.0.0.1"

tests/dibi/Driver.mysqli.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Dibi\Drivers\MySqliDriver;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/bootstrap.php';
9+
10+
//$rc = new \mysqli(
11+
// $config['host'],
12+
// $config['username'],
13+
// $config['password'],
14+
// $config['database'],
15+
// $config['port'],
16+
//);
17+
$rc = new \mysqli(
18+
"127.0.0.1",
19+
'root',
20+
'root',
21+
'dibi_test',
22+
3306,
23+
);
24+
25+
$driver = new MySqliDriver([
26+
'resource' => $rc,
27+
]);
28+
29+
// sanity check
30+
Assert::same($rc, $driver->getResource());
31+
32+
// close the connection
33+
$rc->close();
34+
35+
// This would trigger an error in PHP 8, see #409
36+
Assert::null($driver->getResource());

0 commit comments

Comments
 (0)