public function backupDb($filename, $tables = '*')
    {
        $data = [];

        // get all of the tables
        if ($tables == '*') {
            $tables = array();
            $result = $this->connection->prepare('SHOW TABLES');
            $result->execute();
            while ($row = $result->fetch(PDO::FETCH_NUM)) {
                $tables[] = $row[0];
            }
        } else {
            $tables = \is_array($tables) ? $tables : explode(',', $tables);
        }

        $data[] = "\n";
        $data[] = 'SET foreign_key_checks=0;';
        $data[] = "\n\n";

        foreach ($tables as $table) {
            if (!preg_match("/^[\w_\-]+$/", $table)) {
                Tlog::getInstance()->alert(
                    sprintf(
                        "Attempt to backup the db with this invalid table name: '%s'",
                        $table
                    )
                );

                continue;
            }

            $result = $this->execute('SELECT * FROM `' . $table . '`');

            $fieldCount = $result->columnCount();

            $data[] = 'DROP TABLE `' . $table . '`;';

            $resultStruct = $this->execute('SHOW CREATE TABLE `' . $table . '`');

            $rowStruct = $resultStruct->fetch(PDO::FETCH_NUM);

            $data[] = "\n\n";
            $data[] = $rowStruct[1];
            $data[] = ";\n\n";

            for ($i = 0; $i < $fieldCount; $i++) {
                while ($row = $result->fetch(PDO::FETCH_NUM)) {
                    $data[] = 'INSERT INTO `' . $table . '` VALUES(';
                    for ($j = 0; $j < $fieldCount; $j++) {
                        $row[$j] = addslashes($row[$j]);
                        $row[$j] = str_replace("\n", "\\n", $row[$j]);
                        if (isset($row[$j])) {
                            $data[] = '"' . $row[$j] . '"';
                        } else {
                            $data[] = '""';
                        }
                        if ($j < ($fieldCount - 1)) {
                            $data[] = ',';
                        }
                    }
                    $data[] = ");\n";
                }
            }
            $data[] = "\n\n\n";
        }

        $data[] = 'SET foreign_key_checks=1;';

        //save filename
        $this->writeFilename($filename, $data);
    }
public function backupDb($filename, $tables = '*')
    {
        $data = [];

        // get all of the tables
        if ($tables == '*') {
            $tables = array();
            $result = $this->connection->prepare('SHOW TABLES');
            $result->execute();
            while ($row = $result->fetch(PDO::FETCH_NUM)) {
                $tables[] = $row[0];
            }
        } else {
            $tables = \is_array($tables) ? $tables : explode(',', $tables);
        }

        $data[] = "\n";
        $data[] = 'SET foreign_key_checks=0;';
        $data[] = "\n\n";

        foreach ($tables as $table) {
            if (!preg_match("/^[\w_\-]+$/", $table)) {
                Tlog::getInstance()->alert(
                    sprintf(
                        "Attempt to backup the db with this invalid table name: '%s'",
                        $table
                    )
                );

                continue;
            }

            $result = $this->execute('SELECT * FROM `' . $table . '`');

            $fieldCount = $result->columnCount();

            $data[] = 'DROP TABLE `' . $table . '`;';

            $resultStruct = $this->execute('SHOW CREATE TABLE `' . $table . '`');

            $rowStruct = $resultStruct->fetch(PDO::FETCH_NUM);

            $data[] = "\n\n";
            $data[] = $rowStruct[1];
            $data[] = ";\n\n";

            for ($i = 0; $i < $fieldCount; $i++) {
                while ($row = $result->fetch(PDO::FETCH_NUM)) {
                    $data[] = 'INSERT INTO `' . $table . '` VALUES(';
                    for ($j = 0; $j < $fieldCount; $j++) {
                        $row[$j] = addslashes($row[$j]);
                        $row[$j] = str_replace("\n", "\\n", $row[$j]);
                        if (isset($row[$j])) {
                            $data[] = '"' . $row[$j] . '"';
                        } else {
                            $data[] = '""';
                        }
                        if ($j < ($fieldCount - 1)) {
                            $data[] = ',';
                        }
                    }
                    $data[] = ");\n";
                }
            }
            $data[] = "\n\n\n";
        }

        $data[] = 'SET foreign_key_checks=1;';

        //save filename
        $this->writeFilename($filename, $data);
    }
public function backupDb ( $ filename , $ tables = '*' ) { $ data = [ ] ; // get all of the tables if ( $ tables == '*' ) { $ tables = array ( ) ; $ result = $ this -> connection -> prepare ( 'SHOW TABLES' ) ; $ result -> execute ( ) ; while ( $ row = $ result -> fetch ( PDO :: FETCH_NUM ) ) { $ tables [ ] = $ row [ 0 ] ; } } else { $ tables = \ is_array ( $ tables ) ? $ tables : explode ( ',' , $ tables ) ; } $ data [ ] = "\n" ; $ data [ ] = 'SET foreign_key_checks=0;' ; $ data [ ] = "\n\n" ; foreach ( $ tables as $ table ) { if ( ! preg_match ( "/^[\w_\-]+$/" , $ table ) ) { Tlog :: getInstance ( ) -> alert ( sprintf ( "Attempt to backup the db with this invalid table name: '%s'" , $ table ) ) ; continue ; } $ result = $ this -> execute ( 'SELECT * FROM `' . $ table . '`' ) ; $ fieldCount = $ result -> columnCount ( ) ; $ data [ ] = 'DROP TABLE `' . $ table . '`;' ; $ resultStruct = $ this -> execute ( 'SHOW CREATE TABLE `' . $ table . '`' ) ; $ rowStruct = $ resultStruct -> fetch ( PDO :: FETCH_NUM ) ; $ data [ ] = "\n\n" ; $ data [ ] = $ rowStruct [ 1 ] ; $ data [ ] = ";\n\n" ; for ( $ i = 0 ; $ i < $ fieldCount ; $ i ++ ) { while ( $ row = $ result -> fetch ( PDO :: FETCH_NUM ) ) { $ data [ ] = 'INSERT INTO `' . $ table . '` VALUES(' ; for ( $ j = 0 ; $ j < $ fieldCount ; $ j ++ ) { $ row [ $ j ] = addslashes ( $ row [ $ j ] ) ; $ row [ $ j ] = str_replace ( "\n" , "\\n" , $ row [ $ j ] ) ; if ( isset ( $ row [ $ j ] ) ) { $ data [ ] = '"' . $ row [ $ j ] . '"' ; } else { $ data [ ] = '""' ; } if ( $ j < ( $ fieldCount - 1 ) ) { $ data [ ] = ',' ; } } $ data [ ] = ");\n" ; } } $ data [ ] = "\n\n\n" ; } $ data [ ] = 'SET foreign_key_checks=1;' ; //save filename $ this -> writeFilename ( $ filename , $ data ) ; }
Backup the db OR just a table

@param string $filename
@param string $tables
Backup the db OR just a table