export/import mysql without PhpMyadmin

I was wondering if there is a way to export and import mysql database into .sql file without using PhpMyAdmin, if possible I would like to have php code to do that.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

See http:Q_21209155.html There a lot of links to such solutions.
If you don't care to see the output of the shell command, then try this:


exec("mysqldump -u root dbname > output.sql");


If you don't redirect the mysqldump command's output to a file, then your PHP script will hang until the command is done... if your PHP script timeout is not long enough, then you will get an error.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
villechithAuthor Commented:
no, I have tried it, it doesn't work.  I exported the file and import it back in using phpMyAdmin, but it said there is some error in the code.  

As for the second comment, since I'm not the admin or root, I am just a user, so I have no access to the shell.

I was looking for a very simple short php code to export not just the data structure, but also the data as well. By the way, there are so many code, I'm not sure if I use the right one, but I got the output as ".sql" , but the only thing is when I import it, there is a error.

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

If you have a username and password that gives you access to the database, then you should be able to run the command I gave you earlier, you just have to modify it to look like this (look at your database connect statement):

<?PHP exec("mysqldump -u username -p password > output.sql"); ?>

Otherwise, if you want to do it without calling external commands, check out this following thread... someone asked the same question...


Hope this helps...
villechithAuthor Commented:
hmmmm...I don't know if it's because of me or the code or perhaps with the sql database, but I used to export both the data and structure into sql file and import it back and it worked perfectly. but when I tried hernst42's method it gave me the sql file, but only structure and when I tried to import it back in, it said error on my syntax (but why phpMyAdmin seems to work?!).

As for ralwan's method, I tried it but I don't see any file in my directory?????? did the code execute ? where is the sql file goes? perhaps wrong directory? donno...  

anyone know what's going on or have any idea?

If you didn't specify a directory, then I think the output.sql file will just end up in the temp directory of your PHP installation... so specify a directory if you haven't already done so...

You can also try this to see if the command was giving you an error (you should see the output on the screen):

<?PHP passthru("mysqldump -u username -p password > output.sql"); ?>

Notice that I changed the command from "exec" to "passthru"...

villechithAuthor Commented:
anyway, it doesn't really work for me, but it might work for others, that is why I dont want to keep this question open too long, so I will close it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.