If you're importing a sql-file with triggers, functions, stored procedures and other stuff, you'll might be using DELIMITER in MySQL.
Notice: This function assumes that all delimiters are on it's own line, and that "DELIMITER" are in all caps.
<?php
function mysqli_multi_query_file($mysqli, $filename) {
$sql = file_get_contents($filename);
$sql = preg_replace('#/\*.*?\*/#s', '', $sql);
$sql = preg_replace('/^-- .*[\r\n]*/m', '', $sql);
if (preg_match_all('/^DELIMITER\s+(\S+)$/m', $sql, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
$prev = null;
$index = 0;
foreach ($matches as $match) {
$sqlPart = substr($sql, $index, $match[0][1] - $index);
$index = $match[0][1] + strlen($match[0][0]);
if ($prev && $prev[1][0] != ';') {
$sqlPart = explode($prev[1][0], $sqlPart);
foreach ($sqlPart as $part) {
if (trim($part)) { $mysqli->query($part);
}
}
} else {
if (trim($sqlPart)) { $mysqli->multi_query($sqlPart);
while ($mysqli->next_result()) {;}
}
}
$prev = $match;
}
$sqlPart = substr($sql, $index, strlen($sql)-$index);
if ($prev && $prev[1][0] != ';') {
$sqlPart = explode($prev[1][0], $sqlPart);
foreach ($sqlPart as $part) {
if (trim($part)) {
$mysqli->query($part);
}
}
} else {
if (trim($sqlPart)) {
$mysqli->multi_query($sqlPart);
while ($mysqli->next_result()) {;}
}
}
} else {
$mysqli->multi_query($sql);
while ($mysqli->next_result()) {;}
}
}
?>