I need to execute a .sql file which has about 48 tables to create. It is consisting of comments and sql commands which are ending in a ";". Is there a way to run these sql commands taking them to a single string and at once. I mean I need to run the whole file at once using php. I can execute that file line by line using mysql_query function. But I need to do is to execute all of them at once as same as in phpmyadmin.
Is there a way to do it. I need to do this using php. Can anyone help me please?
You can explode your queries
function batch($file){
$queries = explode(";", $file);
foreach($queries as $query){
mysql_query($query);
}
}
You are looking for
mysqli_multi_query
- Executes one or multiple queries which are concatenated by a semicolon. An alternative would be to use the command line client:
mysql -u USERNAME -p DATABASENAME < FILE.SQL
You can probably use that with exec
and/or system
, but then you have to provide the password after -p
. The above command will prompt you for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With