检查开关案例执行是否成功

时间:2017-09-26 08:03:30

标签: php switch-statement

我创建了一个PHP脚本,它将检查连接的数据库中是否有一些预定义的表,如果表没有,那么脚本将创建这些表。

在这个脚本中,我使用switch ... case来创建表,这样可以解决任何错误。

现在我想做的是检查switch ... case是否以任何错误结束。如果没有错误,那么我想触发另一组或sql语句,这些语句将创建一些每天要运行的任务。

这是我的表检查和创建PHP部分,

<?php
//This is the array that contains all the table names
    $tableNames = array(
        'catdb', 'clientdb', 'qcerrors', 'searchterm', 'teams', 'tempdb', 'userlogin', 'userroles', 'usertimetrack'
    );

    //I'm using this for loop to loop through the array and create the tables
    //if they doesn't exists.
    for ($i = 0; $i < count($tableNames); $i++) {
        $tableCheck = $dbConnect->query("SHOW TABLES LIKE '" . $tableNames[$i] . "'");
        if ($tableCheck->rowCount() > 0) {
            echo $tableNames[$i] . " .... Exists <br />";
        } else {
            $tblName = $tableNames[$i];

            switch ($tblName) {
                case "catdb":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `catdb` (
                                        `catId` int(100) NOT NULL AUTO_INCREMENT,
                                        `uId` int(100) NOT NULL,
                                        `Catagory` varchar(100) NOT NULL,
                                        `createDate` varchar(100) NOT NULL,
                                        PRIMARY KEY (`catId`)
                                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "clientdb":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `clientdb` (
                                        `clientId` int(100) NOT NULL AUTO_INCREMENT,
                                        `catId` varchar(100) NOT NULL,
                                        `uId` int(100) NOT NULL,
                                        `Client` varchar(100) NOT NULL,
                                        `cDate` varchar(100) NOT NULL,
                                        PRIMARY KEY (`clientId`)
                                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "qcerrors":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `qcerrors` (
                                    `qcId` int(100) NOT NULL AUTO_INCREMENT,
                                    `qcError` varchar(100) NOT NULL,
                                    `createdBy` varchar(100) DEFAULT NULL,
                                    `createdOn` varchar(100) DEFAULT NULL,
                                    PRIMARY KEY (`qcId`)
                                    ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "searchterm":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `searchterm` (
                                    `sId` int(100) NOT NULL AUTO_INCREMENT,
                                    `sDate` varchar(100) NOT NULL,
                                    `sUid` int(100) NOT NULL,
                                    `searchedBy` int(100) NOT NULL,
                                    PRIMARY KEY (`sId`)
                                    ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "teams":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `teams` (
                                        `tId` int(100) NOT NULL AUTO_INCREMENT,
                                        `TeamName` varchar(100) NOT NULL,
                                        `tlName` varchar(100) DEFAULT NULL,
                                        `tlSet` varchar(100) NOT NULL,
                                        PRIMARY KEY (`tId`)
                                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "tempdb":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `tempdb` (
                                        `tempId` int(100) NOT NULL AUTO_INCREMENT,
                                        `uId` int(100) NOT NULL,
                                        `tId` int(100) NOT NULL,
                                        `catId` int(100) NOT NULL,
                                        `clientId` int(100) NOT NULL,
                                        `startTime` varchar(100) NOT NULL,
                                        `Status` varchar(100) NOT NULL,
                                        PRIMARY KEY (`tempId`)
                                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "userlogin":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `userlogin` (
                                        `uId` int(100) NOT NULL AUTO_INCREMENT,
                                        `uCreateDate` varchar(100) NOT NULL,
                                        `createdBy` int(100) NOT NULL,
                                        `fName` varchar(100) NOT NULL,
                                        `lName` varchar(100) NOT NULL,
                                        `uName` varchar(100) NOT NULL,
                                        `pWord` varchar(100) NOT NULL,
                                        `uTeam` int(100) NOT NULL,
                                        `uRole` varchar(100) NOT NULL,
                                        PRIMARY KEY (`uId`)
                                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "userroles":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `userroles` (
                                        `urId` int(100) NOT NULL AUTO_INCREMENT,
                                        `userRole` varchar(100) NOT NULL,
                                        PRIMARY KEY (`urId`)
                                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                case "usertimetrack":
                    try {
                        $tblCreate = "CREATE TABLE IF NOT EXISTS `usertimetrack` (
                                        `utId` int(100) NOT NULL AUTO_INCREMENT,
                                        `jDate` varchar(100) NOT NULL,
                                        `usrId` int(100) NOT NULL,  
                                        `Category` varchar(100) NOT NULL,
                                        `utClient` varchar(100) NOT NULL,
                                        `jType` varchar(100) NOT NULL,
                                        `startTime` varchar(100) NOT NULL,
                                        `endTime` varchar(100) NOT NULL,
                                        `timeSpent` varchar(100) NOT NULL,
                                        `Volume` int(100) NOT NULL,
                                        `qcErrorId` varchar(100) NOT NULL,
                                        `noOfProductLines` int(100) DEFAULT NULL,
                                        `Remarks` varchar(1000) DEFAULT NULL,
                                        PRIMARY KEY (`utId`)
                                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1";
                        $dbConnect->exec($tblCreate);
                        echo $tblName . " .... Created OK <br />";
                    } catch (PDOException $e) {
                        echo $tblCreate . "<br>" . $e->getMessage();
                    }
                    break;

                default:
                    echo $tblName . " this table is not part of this system.<br />";
            }
        }
    }
    ?>

现在上述代码运行后没有出现任何错误,我想在SQL语句下运行,这将创建任务。

<?php
$timeNow = date("Y-m-d H:i:s"); //To get the current time
//Set the event scheduler status from OFF to ON
$dbConnect->exec("SET GLOBAL event_scheduler = ON");

//Creates the daily task 1 to truncate searchterm table
$createTask1 = "CREATE DEFINER= '" . $dbUser . "'@`%` EVENT `searcTerm Table Clean` ON SCHEDULE EVERY 1 DAY STARTS '" . $timeNow . "' ON COMPLETION NOT PRESERVE ENABLE DO TRUNCATE TABLE searchterm";
$dbConnect->exec($createTask1);

//Creates the daily task 1 to truncate tempdb table
$createTask2 = "CREATE DEFINER= '" . $dbUser . "'@`%` EVENT `EmptyData` ON SCHEDULE EVERY 1 DAY STARTS '" . $timeNow . "' ON COMPLETION NOT PRESERVE ENABLE DO TRUNCATE TABLE tempdb";
$dbConnect->exec($createTask2);
?>

我确实搜索了Google和SO,但没有看到答案,这就是为什么我决定提问。我希望我已经向社群明确了我的问题,如果有的话,有人可以说明如何做以检查switch ... case结束没有任何错误。

1 个答案:

答案 0 :(得分:2)

在你的代码中,我看到catch块中有很多回声:

} catch (PDOException $e) {
    echo $tblCreate . "<br>" . $e->getMessage();
}

在switch语句之前,你可以创建一个错误数组:

$errors = array();

然后,在catch块中,如果愿意,可以保留echo,但是将错误消息添加到errors数组中:

} catch (PDOException $e) {
    echo $tblCreate . "<br>" . $e->getMessage();
    $errors[] = $tblCreate.' - '.$e->getMessage();
}

最后,当代码运行完毕后,您可以通过计算错误数组来检查错误:

if (count($errors) == 0) {
    // code ran successfully
} else {
    // fail
}