Skip to content

Instantly share code, notes, and snippets.

@chappy84
Last active August 29, 2015 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chappy84/bcebd03d91f208cd5fcd to your computer and use it in GitHub Desktop.
PHP PDO Mysql Multiple Query Bug Debugger. Bug Page: https://bugs.php.net/bug.php?id=61613
<?php
$myConn = new PDO(
'mysql:host=localhost;dbname=dbname',
'username',
'password'
);
$myConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$multiSqlStatement = '
INSERT INTO users (username) VALUES (:username);
INSERT INTO groups (name, type) VALUES (:group_name, :group_type);
INSERT INTO logs (entry) VALUES (NOW());
';
$bindVariables = array(
':group_type' => 'this_type',
':username' => 'my_username',
':group_name' => 'Other'
);
$tempSql = preg_replace('/[\n\r\s]+/', ' ', $multiSqlStatement);
$tempSqlParts = explode(';', $tempSql);
foreach ($tempSqlParts as $tempSqlPart) {
$tempSqlPart = trim($tempSqlPart);
if (!empty($tempSqlPart)) {
$tempBindVars = (0 < preg_match_all('/\:[a-z0-9_]+/i', $tempSqlPart, $matches))
? array_intersect_key($bindVariables, array_flip($matches[0]))
: array();
$tempStatement = $myConn->prepareStatement($tempSqlPart);
$tempStatement->execute($tempBindVars);
$tempStatement->closeCursor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment