473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to restore a mysql dump with php

348 100+
Hi all,

I am back again with another question.

I have an sql file that has INSERT INTO statements in the file. I would like to plug that file into an input box, press a "Restore" button and have that file restore the database. I can get it to work.

I thought I could just use php's exec() function but that doesn't work either. What I need to do is the same as:

mysql -u root -p pass < mySQLFile.sql

Here is what I have managed to scrounge up so far for php

[php]
$back = fopen($_POST['file'],"r");
$contents = fread($back, filesize($_POST['file']));
$res = mysql_query(utf8_encode($contents));

Im lost from here....

fclose($back);[/php]

I have looked and searched for a script or a class to show me how to do this but have come up empty handed.

Could someone please give me a hand?

Thanks,

Frank
Sep 17 '07 #1
3 4061
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. mysql -u root -p pass < mySQLFile.sql
I experimented with the mysqldump commands on different servers
I was more successful with the exec() command than system() .
The full path is sometimes required to the mysqldump folder
The same is true for msql.exe folder and/or the backup file.
Don't leave a gap between -p and the actual password.
Try using with the hostname -h
With some trial and error this works and is a system function written for a specific purpose and is prefered to writing your own
The following worked on my local server
[PHP]fullpath/mysqldump --opt -u username -h hostname -ppassword databasename tablenames > fullbackuppath/filename 2>&1[/PHP]
[PHP]fullpath/mysql -u username -ppassword databasename < filename[/PHP]
Sep 17 '07 #2
fjm
348 100+
Expand|Select|Wrap|Line Numbers
  1. mysql -u root -p pass < mySQLFile.sql
I experimented with the mysqldump commands on different servers
I was more successful with the exec() command than system() .
The full path is sometimes required to the mysqldump folder
The same is true for msql.exe folder and/or the backup file.
Don't leave a gap between -p and the actual password.
Try using with the hostname -h
With some trial and error this works and is a system function written for a specific purpose and is prefered to writing your own
The following worked on my local server
[PHP]fullpath/mysqldump --opt -u username -h hostname -ppassword databasename tablenames > fullbackuppath/filename 2>&1[/PHP]
[PHP]fullpath/mysql -u username -ppassword databasename < filename[/PHP]
Hi Code Green. Thanks for the help..

I have done exactly what you have suggested and I can't get it to work. I have tried with the -h and -p options. BTW: My db does not use a password so I just left the -p option out.

What error I am getting in the apache log is that the "specified file cannot be found".

What I did was to open cmd (Windows) and put the entire command on the command line. I still receive the same error. Now.... When I put quotes around the file to be uploaded on the command line, it works.

Then what I did was to try and use quotes around my POST variable like this:
[PHP]
$command = 'fullpath/mysql -u username -ppassword databasename < $_POST[uploaded_File]'
exec($command);
[/PHP]

It still fails.. Can someone please show me how to place double quotes around the $_POST[uploaded_File] so that it will not fail?

Thanks,

Frank

EDIT: Ok.. I got it. I escaped the double quotes. I could swear I did that last night and it didn't help. Maybe I was just too tired and made a mistake.
Sep 17 '07 #3
code green
1,726 Expert 1GB
Ok.. I got it. I escaped the double quotes
Glad you solved it.
Maybe I was just too tired and made a mistake
It took me two days trying to create a mysqldump on a network server,
FTP the file to an online server and dump the database onto that server.
Unfortunately nobody on this forum offered me any help.
My db does not use a password so I just left the -p option out
Apparently, if you leave the -p option in without the password, a password will be demanded, even if there isn't one.
Sep 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
HI, I'm looking for a script that will allow users/admins to have a one click backup solution for a MYSQL Database.. 'BACK DATABASE' button, click and its done... The a restore option, that...
1
by: Rajesh Kapur | last post by:
Hello, We are on MySQL 4.0.21 on linux. We use InnoDB tables and foreign key constraints extensively. The mysqldump backs up the database tables in alphabetical order with foreign key...
2
by: Kees | last post by:
I've a backup from my database running on mysql 4.1.12 Now i want to restore this to an old machine running 3.23. How to do this. I get several errors. Kees
2
by: D. Dante Lorenso | last post by:
First I created a function that selected the next available pin code from a table of pre-defined pin codes: CREATE FUNCTION "public"."get_next_pin_code" () RETURNS varchar AS' DECLARE...
2
by: Jerry LeVan | last post by:
Hi, I am just getting into large objects and bytea "stuff". I created a small db called pictures and loaded some large objects and then tried to do a restore. Here is how I got the dump. ...
1
by: ruben | last post by:
Hi: I'm trying to dump tableA and restore it to tableB: $ ./pg_dump -Fc -t tableA databaseA -f tableA.dump -v $ ./pg_restore -t tableB -d databaseA tableA.dump -v pg_dump creates...
5
by: patrick | last post by:
Mysql 4.1.15 on Win2k. Using InnoDB. Using the mysql administrator gui to create a backup, everything goes fine, and restores quickly. Using the command line: mysqldump %dbname%...
2
by: Vadim | last post by:
Hi! I succesfully exported my locally stored db using the following syntax: mysqldump -u root --password=111 finance > sqlbk.dump Now I created another empty db finance2 (on the same Mysql...
2
by: clearissues | last post by:
Hi All, I have a python script which takes dump of postgres and restores the same. When i take a dump there is no problem. command to take dump: pg_dump -b -c -C --format=c -d -h <<hostname>>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.