Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.