export/import mysql without PhpMyadmin

Posted on 2004-11-17
Last Modified: 2012-05-05
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.
Question by:villechith
    LVL 48

    Assisted Solution

    See http:Q_21209155.html There a lot of links to such solutions.

    Accepted Solution

    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.


    Author Comment

    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.


    Expert Comment

    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...

    Author Comment

    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?


    Expert Comment

    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"...


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now