Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. function getBackup($host = 'localhost',$user = 'root',$pass= '',$name= 'rfid_demo_csts',$tables = '*'){
  2. $return='';
  3. $link = mysqli_connect($host, $user, $pass, $name);
  4.  
  5. if($tables == '*')
  6. {
  7. $tables = array();
  8. $result = mysqli_query($link,'SHOW TABLES');
  9. while($row = mysqli_fetch_row($result))
  10. {
  11. $tables[] = $row[0];
  12. }
  13. }
  14. else
  15. {
  16. $tables = is_array($tables) ? $tables : explode(',',$tables);
  17. }
  18.  
  19. //cycle through
  20. foreach($tables as $table)
  21. {
  22. $result = mysqli_query($link,'SELECT * FROM '.$table);
  23. $num_fields = mysqli_num_fields($result);
  24.  
  25. $return.= 'DROP TABLE IF EXISTS '.$table.';';
  26. $row2 = mysqli_fetch_row(mysqli_query($link,'SHOW CREATE TABLE '.$table));
  27.  
  28. $return.= "\n\n".$row2[1].";\n\n";
  29.  
  30. for ($i = 0; $i < $num_fields; $i++)
  31. {
  32. while($row = mysqli_fetch_row($result))
  33. {
  34. $return.= 'INSERT INTO '.$table.' VALUES(';
  35. for($j=0; $j < $num_fields; $j++)
  36. {
  37. $row[$j] = addslashes($row[$j]);
  38. if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
  39. if ($j < ($num_fields-1)) { $return.= ','; }
  40. }
  41. $return.= ");\n";
  42. }
  43. }
  44. $return.="\n\n\n";
  45. }
  46.  
  47. $handle = fopen('#DB_BACKUP/DBCSTS_'.date("Ymd").'.sql','w+');
  48. fwrite($handle,$return);
  49. fclose($handle);
  50.  
  51. //add below code to download it as a sql file
  52. Header('Content-type: application/sql');
  53. Header('Content-Disposition: attachment; filename=DBCSTS_'.date("Ymd").'.sql','w+');
  54. echo $return;
  55.  
  56. $data['title'] = 'EXPORT';
  57. $data['sub_title'] = 'DATA';
  58. return view('user.exportSuccess', $data);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement