J'ai créé un script PHP
qui va vérifier la base de données connectée pour certaines tables prédéfinies et si les tables ne sont pas là, le script va créer ces tables.Vérification de l'exécution réussie du commutateur
Dans ce script, j'ai utilisé un switch ... case
pour créer les tables, cela fonctionne sans aucune erreur.
Maintenant, ce que je veux faire est de vérifier si le switch ... case
s'est terminé sans aucune erreur. S'il n'y a pas d'erreur, je veux déclencher un autre ensemble ou des instructions sql
qui vont créer des tâches à exécuter tous les jours.
Ceci est ma table et vérifier la création PHP
partie,
<?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 />";
}
}
}
?>
Maintenant, après le code ci-dessus fonctionne avec toute erreur que je veux exécuter ci-dessous SQL
déclarations qui vont créer les tâches. J'ai fait une recherche google et SO, mais je n'ai pas vu une réponse, c'est pourquoi j'ai décidé de poser la question. J'espère avoir rendu mon problème clair à la communauté si quelqu'un peut jeter quelque lumière sur ce qu'il faut faire pour vérifier le switch ... case
terminé avec aucune erreur.
MyISAM? Vraiment? – GordonM
Pouvez-vous m'en dire un meilleur alors, s'il vous plaît utilisait le moteur par défaut dans 'MySQL'. – Sand
InnoDB est la valeur par défaut pour MySQL depuis des années. Vous utilisez une version relativement récente de MySQL, n'est-ce pas? En outre, Latin 1 pourrait venir vous hanter à l'avenir, pourquoi ne pas utiliser UFT8MB4? – GordonM