Connecting Tech Pros Worldwide Forums | Help | Site Map

run mysql dump code via a form

Member
 
Join Date: Feb 2007
Posts: 75
#1: Jun 19 '09
I want to run the mysql generated dump values of a database through a textarea in php. I am copying the whole dump code in a textarea and then let the php run the entire code. But here the problem is that the unwanted tags or lines like the mysql comments needs to be ignored and only the actual sql queries should run. How do i do it?

May be this is not very clear. Well, i have a database which needs to be updated in every 3 or 6 months for which the actual data is collected from another source in excel format. I convert this excel data into mysql format in my local machine and then the data needs to be updated in my ISP's server for which i am creating a form through which the updates can be done. I want the updates to be done online without myself having to go to my ISP. So in this case phpmyadmin generates me the mysqldump sql file but when i copy and run the whole contents of the file, it has commented tags which php donot ignore. It runs along with the commented tags as a result the database is never updated. I want a solution to ignore the commented tags so that php runs only the genuine sql queries.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Jun 19 '09

re: run mysql dump code via a form


You could save the entire thing to a temporary file on the server and import it through exec.

Like:
Expand|Select|Wrap|Line Numbers
  1. if(file_put_contents($tempFile, $_POST['dump'])) 
  2. {
  3.     exec("mysql --user={$mysqlUser} --password={$mysqlPass} {$targetDatabase} < {$tempFile}", $out, $failed);
  4.  
  5.     if($failed) {
  6.         echo "Import failed!";
  7.     }
  8.     else {
  9.         echo "Your backup has been successfully imported!";
  10.     }
  11.  
  12.     // Delete the dump file
  13.     unlink($tempFile);
  14. }
Some hosts might not allow the use of exec, but it's worth a try.
Reply