Take the 2-minute tour ×

How can I call the equivalent line in PHP for this MySql line:

mysql> source [path-of-file].sql
share|improve this question
This question seems fairly similar to yours... –  optimusprime619 Apr 16 '12 at 12:18
This is non-trivial, because it involves parsing the queries with PHP. PHP is unable to execute more than one query at once. You might be able to $queries = explode(';', file_get_contents('[path-of-file].sql')); foreach ($queries as $query) mysql_query($query);, but if any of the queries contains a literal ; or changes the delimiter at any point in the file, this will not work. –  DaveRandom Apr 16 '12 at 12:22
Don't pass the password on commandline, if you choose to go that road. Setup passwordless login instead, see here for more information. –  Berry Langerak Apr 16 '12 at 12:25
add comment

1 Answer

up vote 1 down vote accepted

SOURCE is not a native mySQL command, but one specific to the command line client.

In PHP you could try something like:

shell( "mysql --user=$user --password=$password $database < $file" );
share|improve this answer
Yeah, passing $password in a command is really what you should be doing ;) Rather create a ~/.my.cnf file including the password, and remove read-permissions for anyone but the user PHP is running as. That said; it's a viable (if not shaky) solution, so +1. –  Berry Langerak Apr 16 '12 at 12:24
I am assuming its just a once off quick setup instead of a permanent solution :P –  Brendan Scarvell Apr 16 '12 at 12:26
Although I understand this is probably a one-off solution, I'd still never recommend passing a password ;) –  Berry Langerak Apr 16 '12 at 12:26
Thanks. There are two of us developing and therefore we will be running the source command multiple times. Yes will add the password to the my.cnf file. –  Prakash Raman Apr 16 '12 at 13:02
add comment

Your Answer


