Skip to content

Instantly share code, notes, and snippets.

@nikic

nikic/pdo.php Secret

Created June 8, 2021 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikic/a55b70d6b7c1acf6785c7e50c37754dc to your computer and use it in GitHub Desktop.
Save nikic/a55b70d6b7c1acf6785c7e50c37754dc to your computer and use it in GitHub Desktop.
<?php
class PDOConnection extends PDO {
public function prepare($prepareString, $driverOptions = []): PDOStatement|false
{
return parent::prepare($prepareString, $driverOptions);
}
}
print(memory_get_usage() . "\n");
$dbh = new PDOConnection('sqlite:memory');
//$dbh->exec('CREATE TABLE user (active BOOL, admin BOOL, created_at DATE, updated_at DATE)');
print(memory_get_usage() . "\n");
for($i = 0; $i < 100; $i++) {
// Obviously we normally would not generate a new identical prepared statement inside
// of a loop, but this demonstrates the problem well, and an ORM using PDOConnection
// (e.g. Laravel Eloquent) does actually generate a new prepared statement for each
// query, expecting that the statement will be properly destroyed once all references
// to it are destroyed.
$stmt = $dbh->prepare(
"INSERT INTO user (active, admin, created_at, updated_at) VALUES (?, ?, ?, ?)");
if($i == 0) {
print(memory_get_usage() . "\n");
}
$stmt->execute([1, 1, '2018-01-01', '2018-01-01']);
if($i == 0) {
print(memory_get_usage() . "\n");
}
}
$stmt = null;
print(memory_get_usage() . "\n");
$dbh = null;
print(memory_get_usage() . "\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment