Hello
I tried to write a function that takes a parameter content of an SQL file and executes the statements in it, one after the other.
Here is an example of such content
[code]
# Drop all the tables
DROP TABLE IF EXISTS `table1`;
DROP TABLE IF EXISTS `table2`;
# Create empty tables
CREATE TABLE `table1` (
`ID` TINYINT(1) unsigned NOT NULL,
`Param` varchar(255) default ”,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `table2` (
`ID` TINYINT(1) unsigned NOT NULL,
`Param` varchar(255) default ”,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `table1` (`ID`, `Param`) VALUES (1, ‘ABC’);
INSERT INTO `table2` (`ID`, `Param`) VALUES (1, ‘abc’);
Here is my function:
[code=php]
function runBatchOnConnection($connection, $batchContent) {
$commands = explode(‘;’, $batchContent);
foreach ($commands as $command) {
$command = trim($command);
if ($command == ”) {
continue;
}
$command = stripslashes($command);
$result = mysql_query($command, $connection);
if ($result === false) {
trigger_error(“Error in executing query: ” . $command . “rn” . mysql_error($connection), E_USER_ERROR);
exit;
}
}
}
The problem with this function is that it fails on an SQL statement which contains a ‘;’. For example:
[code]
INSERT INTO `table2` (`ID`, `Param`) VALUES (2, ‘;’);
Can anyone show me how it could be fixed?
regards