I'm creating an installation script for an application that I'm developing and need to create databases dynamically from within PHP. I've got it to create the database but now I need to load in several .sql files. I had planned to open the file and mysql_query it a line at a time - until I looked at the schema files and realised they aren't just one query per line.
So, how do I load an sql file from within PHP (as phpMyAdmin does with its import command)?
mysqli
can run multiple queries separated by a;
you could read in the whole file and run it all at once using
mysqli_multi_query()
But, I'll be the first to say that this isn't the most elegant solution.
Some PHP libraries can parse a SQL file made of multiple SQL statements, explode it properly (not using a simple ";" explode, naturally), and the execute them.
For instance, check Phing's PDOSQLExecTask
This The Best Code For restore sql by php can use 100% Goooood! Thank A lot
Some guys (Plahcinski) suggested this code:
but I would update it with the one which worked for me:
because it is more comprehensive. ;-)
I hope the following code will solve your problem pretty well.