2016-10-15 6 views
0

ボタンをクリックしてデータベースをダウンロードしたい。私は周りを検索し、このコードを発見した。 yii2 database downloadYii2データベースのダウンロード方法

コントローラにアクションを追加し、この変更されたコードを追加しました。私は元のコードでデータベース接続を削除しましたが、私は再度接続する必要はないと思います。私は 'fetch_row()'でエラーになります。 Yii::$app->db->createCommand('fetch_row')を試しましたが、まだエラーが発生しています。

define('BACKUP_DIR', 'download/') ; 
     // Define Database Credentials 
     define('HOST', 'localhost') ; 
     define('USER', 'root') ; 
     define('PASSWORD', 'x24613rt') ; 
     define('DB_NAME', 'panthoibi') ; 
     $files = scandir(BACKUP_DIR); 
     if(count($files) > 2) { 
      for ($i=2; $i < count($files); $i++) { 
       unlink(BACKUP_DIR."/".$files[$i]); 
      } 
     } 
     /* 
     Define the filename for the sql file 
     If you plan to upload the file to Amazon's S3 service , use only lower-case letters 
     */ 
     $fileName = 'mysqlibackup--' . date('d-m-Y') . '@'.date('h.i.s').'.sql' ; 
     // Set execution time limit 
     if(function_exists('max_execution_time')) { 
      if(ini_get('max_execution_time') > 0)  
       set_time_limit(0) ; 
     } 

     // Check if directory is already created and has the proper permissions 
     if (!file_exists(BACKUP_DIR)) mkdir(BACKUP_DIR , 0700) ; 
     if (!is_writable(BACKUP_DIR)) chmod(BACKUP_DIR , 0700) ; 

     // Create an ".htaccess" file , it will restrict direct accss to the backup-directory . 
     //$content = 'deny from all' ; 
     //$file = new SplFileObject(BACKUP_DIR . '/.htaccess', "w") ; 
     //$file->fwrite($content) ; 


     // Introduction information // 
     $return = ""; 
     $return .= "--\n"; 
     $return .= "-- A mysqli Backup System \n"; 
     $return .= "--\n"; 
     $return .= '-- Export created: ' . date("Y/m/d") . ' on ' . date("h:i") . "\n\n\n"; 
     $return = "--\n"; 
     $return .= "-- Database : " . DB_NAME . "\n"; 
     $return .= "--\n"; 
     $return .= "-- --------------------------------------------------\n"; 
     $return .= "-- ---------------------------------------------------\n"; 
     $return .= 'SET AUTOCOMMIT = 0 ;' ."\n" ; 
     $return .= 'SET FOREIGN_KEY_CHECKS=0 ;' ."\n" ; 
     $tables = array() ; 

     // Exploring what tables this database has 
     $result = Yii::$app->db->createCommand('SHOW TABLES'); 

     // Cycle through "$result" and put content into an array 
     while ($row = $result->fetch_row()) 
      $tables[] = $row[0] ; 

     // Cycle through each table 
     foreach($tables as $table) 
     { 
      // Get content of each table 
      $result = $mysqli->query('SELECT * FROM '. $table) ; 
      // Get number of fields (columns) of each table 
      $num_fields = $mysqli->field_count ; 
      // Add table information 
      $return .= "--\n" ; 
      $return .= '-- Tabel structure for table `' . $table . '`' . "\n" ; 
      $return .= "--\n" ; 
      $return.= 'DROP TABLE IF EXISTS `'.$table.'`;' . "\n" ; 
      // Get the table-shema 
      $shema = $mysqli->query('SHOW CREATE TABLE '.$table) ; 
      // Extract table shema 
      $tableshema = $shema->fetch_row() ; 
      // Append table-shema into code 
      $return.= $tableshema[1].";" . "\n\n" ; 
      // Cycle through each table-row 
      while($rowdata = $result->fetch_row()) 
      { 
       // Prepare code that will insert data into table 
       $return .= 'INSERT INTO `'.$table .'` VALUES (' ; 
       // Extract data of each row 
       for($i=0; $i<$num_fields; $i++) 
        $return .= '"'.$rowdata[$i] . "\"," ; 
       // Let's remove the last comma 
       $return = substr("$return", 0, -1) ; 
       $return .= ");" ."\n" ; 
      } 
      $return .= "\n\n" ; 
     } 

     $return .= 'SET FOREIGN_KEY_CHECKS = 1 ; ' . "\n" ; 
     $return .= 'COMMIT ; ' . "\n" ; 
     $return .= 'SET AUTOCOMMIT = 1 ; ' . "\n" ; 
     //$file = file_put_contents($fileName , $return) ; 
     $zip = new ZipArchive() ; 
     $resOpen = $zip->open(BACKUP_DIR . '/' .$fileName.".zip" , ZIPARCHIVE::CREATE) ; 
     if($resOpen) 
      $zip->addFromString($fileName , "$return") ; 
     $zip->close() ; 
     $fileSize = $this->get_file_size_unit(filesize(BACKUP_DIR . "/". $fileName . '.zip')) ; 

答えて

0

あなたはmysqliのコマンドでのYii queryコマンドを混合している、これは動作しません。..

例:

あなたはCreateCommand

// Exploring what tables this database has 
    $result = Yii::$app->db->createCommand('SHOW TABLES'); 

を使用しているが、実行はしません。お試しください。

$result = Yii::$app->db->createCommand('SHOW TABLES')->execute(); 

または

$result = Yii::$app->db->createCommand('SHOW TABLES')->queryOne(); 

または

$result = Yii::$app->db->createCommand('SHOW TABLES')->queryAll(); 

そして、例えば$結果内容を見てとる試してみてください。

var_dump($result); 

あなたの$結果コンテンツは

のために適応させることがshoulはありませんが
$result->fetch_row() 

bu最終的にPHP関数を使用して実際の$結果のコンテンツを反復または管理することができます..

+0

'$ result = Yii :: $ app-> db-> createCommand(' SHOW TABLES ') - > execute();'私は 'var_dump($ result);'を追加しました。私は結果を見るために他のものをすべて削除しました。結果は 'int(23)1'です。これは何かを表していますか?ありがとうございました! – gojiraki

+0

これは、コマンドが実行されることを意味します(しかし、私は思うように、SQLのコマンドを表示しないでください)。私は他の実行方法と答えを更新しています..これらがあなたに良い結果を与えるかどうか試してみてください。 – scaisEdge

関連する問題