Last active
October 25, 2018 10:21
-
-
Save OJezu/77753681c38e4fb7100b979ab71443d8 to your computer and use it in GitHub Desktop.
PHP IS SOOO GREAT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPDO(): \PDO | |
{ | |
return new \PDO("mysql:dbname=solidsnake_dev;host=db", "solidsnake_dev", "solidsnake_dev", [ | |
PDO::ATTR_PERSISTENT => true, | |
]); | |
} | |
$pdo = getPDO(); | |
$pdo->exec('CREATE TEMPORARY TABLE temp_test (id INT NOT NULL PRIMARY KEY)'); | |
$pdo->exec('INSERT INTO temp_test VALUES (1), (2)'); | |
$pdo2 = getPDO(); | |
try { | |
// SAME CONNECTION IS REUSED WHILE STILL ACTIVE IN FIRST PDO! | |
var_dump($pdo2->query('SELECT * FROM temp_test')->fetchAll(PDO::FETCH_ASSOC)); | |
} catch (\Throwable $exception) { | |
var_dump($exception->getMessage()); | |
} | |
// prevent first connection from getting GC'ed | |
$pdo->exec('SELECT * FROM temp_test'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getPDO(): \PDO | |
{ | |
return new \PDO("mysql:dbname=test;host=localhost", "test", "test", [ | |
PDO::ATTR_PERSISTENT => true, | |
]); | |
} | |
$pdo = getPDO(); | |
$pdo->exec('CREATE TEMPORARY TABLE temp_test (id INT NOT NULL PRIMARY KEY)'); | |
$pdo->exec('INSERT INTO temp_test VALUES (1), (2)'); | |
$pdo = getPDO(); | |
try { | |
var_dump($pdo->query('SELECT * FROM temp_test')->fetchAll(PDO::FETCH_ASSOC)); | |
} catch (\Throwable $exception) { | |
var_dump($exception->getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment