PHP PDO数据库备份类

时间:2015-05-28 11:50:41

标签: php mysql sql database pdo

我正在使用PHP和PDO构建数据库备份类,但不幸的是我遇到了一些问题。

example class that I found

我收到多条错误消息,说我的第二个for循环中的偏移量不正确。此外,当我在$sql上使用Backing up tbl_students table... CREATE DATABASE IF NOT EXISTS plansysteem_keuzetrainingen; USE plansysteem_keuzetrainingen; DROP TABLE IF EXISTS tbl_students; CREATE TABLE `tbl_students` ( `idStudent` int(11) NOT NULL AUTO_INCREMENT, `studentUsualName` varchar(300) NOT NULL, `studentPrefix` varchar(200) DEFAULT NULL, `studentSurname` varchar(300) NOT NULL, `studentNumber` int(11) NOT NULL, `studentClass` varchar(300) NOT NULL, `studentEmailAddress` varchar(300) NOT NULL, PRIMARY KEY (`idStudent`) ) ENGINE=InnoDB AUTO_INCREMENT=261 DEFAULT CHARSET=utf8mb4; INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); Backing up tbl_users table... CREATE DATABASE IF NOT EXISTS plansysteem_keuzetrainingen; USE plansysteem_keuzetrainingen; DROP TABLE IF EXISTS tbl_students; CREATE TABLE `tbl_students` ( `idStudent` int(11) NOT NULL AUTO_INCREMENT, `studentUsualName` varchar(300) NOT NULL, `studentPrefix` varchar(200) DEFAULT NULL, `studentSurname` varchar(300) NOT NULL, `studentNumber` int(11) NOT NULL, `studentClass` varchar(300) NOT NULL, `studentEmailAddress` varchar(300) NOT NULL, PRIMARY KEY (`idStudent`) ) ENGINE=InnoDB AUTO_INCREMENT=261 DEFAULT CHARSET=utf8mb4; INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); INSERT INTO tbl_students VALUES(); DROP TABLE IF EXISTS tbl_users; CREATE TABLE `tbl_users` ( `idUser` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(300) NOT NULL, `userPassWord` varchar(300) NOT NULL, `userPassWordHash` varchar(300) NOT NULL, `userEmailAddress` varchar(300) NOT NULL, PRIMARY KEY (`idUser`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4; INSERT INTO tbl_users VALUES(); 时,我得到以下结果:

tbl_students

我从我的数据库中得到第一个表两次,我无法找出原因,因为我的数据库只包含两个表tbl_users&amp; <?php class DatabaseBackup { private $dbConnection; private $dbName; public function __construct($dbConnection, $dbName) { $this->dbConnection = $dbConnection->dbConnection; $this->dbName = $dbName; } public function backupDbTables($dbTables = "*", $outputDir = ".") { if($dbTables == "*") { $dbTables = array(); $firstResult = $this->dbConnection->query("SHOW TABLES"); while($firstResultRow = $firstResult->fetch(PDO::FETCH_BOTH)) { $dbTables[] = $firstResultRow[0]; } } else { $dbTables = is_array($dbTables) ? $dbTables : explode(',',$dbTables); } $sql = 'CREATE DATABASE IF NOT EXISTS '.$this->dbName.";\n\n"; $sql .= 'USE '.$this->dbName.";\n\n"; foreach($dbTables as $dbTable) { echo "Backing up ".$dbTable." table..."; $selectNumFields = $this->dbConnection->query("SELECT COUNT(*) FROM ".$dbTable); $numFieldsArray = $selectNumFields->fetchColumn(); $numFields = $numFieldsArray; $secondResult = $this->dbConnection->query("SELECT * FROM ".$dbTable); $sql .= 'DROP TABLE IF EXISTS '.$dbTable.';'; $thirdResult = $this->dbConnection->query("SHOW CREATE TABLE ".$dbTable); $thirdResultRows = $thirdResult->fetch(PDO::FETCH_NUM); $sql.= "\n\n".$thirdResultRows[1].";\n\n"; for($i = 0; $i < $numFields; $i++) { while($row = $secondResult->fetch(PDO::FETCH_NUM)) { $sql .= 'INSERT INTO '.$dbTable.' VALUES('; for($j = 0; $j < $numFields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = str_replace("\n","\\n",$row[$j]); if(isset($row[$j])) { $sql .= '"'.$row[$j].'"' ; } else { $sql.= '""'; } if($j < ($numFields-1)) { $sql .= ','; } } $sql.= ");\n"; } } $sql.="\n\n\n"; echo "<pre>"; print_r($sql); echo "</pre>"; } } public function saveBackUpFile() { } } ?> 。您可以在下面找到我的数据库备份类。

DatabaseBackup.class.php

 before_filter :action_a, :action_b, only: [:action_c, :action_d]

我希望有人可以帮我修复这个课程以使其正常工作,如果您对我的代码有任何建议,我希望听到它们。

1 个答案:

答案 0 :(得分:0)

在访问之前,您必须检查数组中是否存在元素!

所以你需要像这样重写你的循环:

for($j = 0; $j < $numFields; $j++) {
    if(isset($row[$j])) {
        $row[$j] = addslashes($row[$j]);
        $row[$j] = str_replace("\n","\\n",$row[$j]);
        $sql .= '"'.$row[$j].'"' ;
    } else {
        $sql.= '""';
    }
    if($j < ($numFields-1)) {
        $sql .= ',';
    }
}