4

If I were to want to create a PHP function that does the same thing as the Export tab in phpMyAdmin, how could I do it? I don't know if there is a MySQL function that does this or if phpMyAdmin just builds the export file (in SQL that is) manually. Without shell access. Just using PHP.

I tried the documentation for mysqldump, but that seemed to require using the shell. I'm not quite sure what that even is -- maybe my question is: how do you use shell?

My silly idea is to allow non-technical users to build a site on one server (say a localhost) using MySQL then export the site, database and all, to another server (eg. a remote server).

I think I'm pretty clear on the Import process.

1 Answer 1

6

You can check the phpMyAdmin source code (an advantage of open-source software). Check the export.php script and the supporting functions in the libraries/export/sql.php script file.

In summary, what phpMyAdmin does is:

  • get a list of the tables in the given database (SHOW TABLES FROM...),
  • get the create query for each table (SHOW CREATE TABLE...),
  • parse it and extract column definitions from it,
  • get all data (SELECT * FROM...)
  • build a query according to column data.

I've written similar code for my own apps (for backup purposes, when the GPL license of phpMyAdmin doesn't allow me to use it), however I use DESCRIBE to get column definitions. I think they rather parse the SHOW CREATE TABLE output because contains more information than DESCRIBE output.

This way to generate SQL sentences requires a bit of care to handle the escaping but it allows for some flexibility, as you can convert types, filter or sanitize data, etc. It is also a lot slower than using a tool like mysqldump and you should take care of not consuming all available memory (write soon, write often, don't keep everything in memory).

If you will implement a migration process (from server to server) maybe it would be easier to do it with some shell scripting and calling mysqldump directly, unless you will do everything with PHP.

5
  • 2
    Thanks, exactly what I was looking for. Yeah, I think, because I know php I'd want to do everything in PHP. It's WordPress-like CMS programming. I figured phpMyAdmin runs a series of steps, but I had to ask before spending a lot of time doing it from scratch. I'm all about reinventing wheel to waste away lots of good Saturdays.
    – Phil Tune
    Commented Jul 9, 2012 at 12:51
  • Great question. I learnt something crazy today. I can't understand why they don't offer mysqldump like functionality over the database connection. Why separate the dumping to another program?
    – Phil
    Commented Mar 29, 2017 at 15:21
  • @Phil_1984_ I have wondered the same thing everytime I have to write code to do it. That would be very useful indeed. I don't know any RDBMS that do it, they have SQL instructions to dump locally (like SELECT... INTO OUTFILE or BACKUP DATABASE) or command-line tools or GUI tools, but nothing to do it thru the network from your own application.
    – MV.
    Commented Mar 29, 2017 at 19:59
  • PhpMyAdmin never use the mysqldump binary from mysql ? Commented Jan 4, 2022 at 18:44
  • 1
    into PMA\libraries\classes\Plugins\Export\ExportSql.php you will find public function exportData()... Commented Jan 4, 2022 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.