Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getBackup($host = 'localhost',$user = 'root',$pass= '',$name= 'rfid_demo_csts',$tables = '*'){
- $return='';
- $link = mysqli_connect($host, $user, $pass, $name);
- if($tables == '*')
- {
- $tables = array();
- $result = mysqli_query($link,'SHOW TABLES');
- while($row = mysqli_fetch_row($result))
- {
- $tables[] = $row[0];
- }
- }
- else
- {
- $tables = is_array($tables) ? $tables : explode(',',$tables);
- }
- //cycle through
- foreach($tables as $table)
- {
- $result = mysqli_query($link,'SELECT * FROM '.$table);
- $num_fields = mysqli_num_fields($result);
- $return.= 'DROP TABLE IF EXISTS '.$table.';';
- $row2 = mysqli_fetch_row(mysqli_query($link,'SHOW CREATE TABLE '.$table));
- $return.= "\n\n".$row2[1].";\n\n";
- for ($i = 0; $i < $num_fields; $i++)
- {
- while($row = mysqli_fetch_row($result))
- {
- $return.= 'INSERT INTO '.$table.' VALUES(';
- for($j=0; $j < $num_fields; $j++)
- {
- $row[$j] = addslashes($row[$j]);
- if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
- if ($j < ($num_fields-1)) { $return.= ','; }
- }
- $return.= ");\n";
- }
- }
- $return.="\n\n\n";
- }
- $handle = fopen('#DB_BACKUP/DBCSTS_'.date("Ymd").'.sql','w+');
- fwrite($handle,$return);
- fclose($handle);
- //add below code to download it as a sql file
- Header('Content-type: application/sql');
- Header('Content-Disposition: attachment; filename=DBCSTS_'.date("Ymd").'.sql','w+');
- echo $return;
- $data['title'] = 'EXPORT';
- $data['sub_title'] = 'DATA';
- return view('user.exportSuccess', $data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement