Halo! I am Sumit, a software architect, consultant, in Bangalore. I have 14+ years of web addiction, 3 failed startups, 8 startups total. You can poke me @sumitdatta

Saturday, June 20, 2009

Import MySQL dump with PHP on webhosts

Webhosts like GoDaddy and other similar often limit how much you can import with the online control panel (phpMyAdmin in most cases). So here is a simple PHP script that does this for you. You have to take a mysql dump from your database. Upload it to you host.

Make sure you change the mysql_connect parameters to reflect your database server settings. The following line numbers need change:

What I have done is simply read the dump line by line. Lines starting with -- or /* are considered comments and left out. Now we need to build full SQL commands which span across many lines in the dump. So we check for the end semicolon (;) and until we find one we just join the lines together to get one SQL statement. Once done we execute it and move on to the next.

If you find any errors please comment. Also if you port this to use PostgreSQL or use other programming languages please share your code's link on this post's comment.