执行php时,只运行5个mysql语句中的1个

时间:2016-04-08 18:24:40

标签: php mysql pdo

我的陈述是这样准备的:

$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!";
}
....

..等等其余的陈述。

我的问题是当我尝试运行ifs中的所有语句时,只运行第一个语句(statement_1)。其余的都没有成功。

我是否需要将所有语句作为一个长字符串运行?还是在交易区?

1 个答案:

答案 0 :(得分: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!";
}
祝你好运