Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. <?php
  2.  
  3. $exportData = [
  4. 'db0' => ['table0', 'table1'],
  5. 'db1' => ['table0']
  6. ];
  7.  
  8. $user = 'test';
  9. $password = 'test123';
  10. $host = '127.0.0.1';
  11. $path = '/tmp';
  12.  
  13. foreach ($exportData as $db => $tables) {
  14. foreach ($tables as $table) {
  15. $csvFile = $path . '/' . $db . '.' . $table . '.csv';
  16.  
  17. $command = 'mysql -u ' . $user . ' -h' . $host . ' -p' . $password . ' -B -e "select * from ' . $db . '.' . $table . ' limit 10;"';
  18. //NOTE: change the separator as required; ',' is not suitable always
  19. $command .= '| sed "s/\'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > ' . $csvFile; //this regex is taken from stackoverflow; can't take its credit
  20. shell_exec($command);
  21.  
  22. if (file_exists($csvFile)) { //remove column header
  23. $command = "sed -i 1d $csvFile";
  24. shell_exec($command);
  25. }
  26.  
  27. echo "Created $csvFile\n";
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement