我的语句是这样准备的:
$statement_1 = $DBH->prepare("
DROP TABLE IF EXISTS `table_1`;
CREATE TABLE `table_1` (
... statements are all running fine in phpmyadmin when executed separately
");
$statement_2 = $DBH->prepare("
DROP TABLE IF EXISTS `table_2`;
CREATE TABLE `table_2` (
... statements are all running fine in phpmyadmin when executed separately
");
$statement_3 = $DBH->prepare("
DROP TABLE IF EXISTS `table_3`;
CREATE TABLE `table_3` (
... statements are all running fine in phpmyadmin when executed separately
");
$statement_4 = $DBH->prepare("
DROP TABLE IF EXISTS `table_4`;
CREATE TABLE `table_4` (
... statements are all running fine in phpmyadmin when executed separately
");
$statement_5 = $DBH->prepare("
DROP TABLE IF EXISTS `table_5`;
CREATE TABLE `table_5` (
... statements are all running fine in phpmyadmin when executed separately
");
然后,我尝试在单独的if语句中执行这些语句,这样我就可以看到所有语句都成功执行了,如下所示:
if($statement_1->execute()){
echo "statement_1 executed successfully!";
}else{
echo "statement_1 failed to execute!";
}
if($statement_2->execute()){
echo "statement_2 executed successfully!";
}else{
echo "statement_2 failed to execute!";
}
....
等等,依此类推。
我的问题是,当我尝试运行if中的所有语句时,只有第一个语句运行(statement_1)。其余的都不成功。
是否需要将所有语句作为一个长字符串运行?还是在交易块中?
完成语句后关闭它…
if($statement_1->execute()){
$statement_1->closeCursor();
echo "statement_1 executed successfully!";
}else{
echo "statement_1 failed to execute!";
}
if($statement_2->execute()){
$statement_2->closeCursor();
echo "statement_2 executed successfully!";
}else{
echo "statement_2 failed to execute!";
}
祝好运