Running MySQL *.sql files in PHP
This question comes up from time to time. There's no good solution for running a .sql script directly from PHP. There are edge cases where statements common in a .sql script can't be executed as SQL statements. For example, the mysql tool has builtin commands that are not recognized by the MySQL Server, e.g. CONNECT
, TEE
, STATUS
, and DELIMITER
.
So I give +1 to @Ignacio Vazquez-Abrams's answer. You should run your .sql script in PHP by invoking the mysql
tool, for instance with shell_exec()
.
I got this test working:
$command = "mysql --user={$vals['db_user']} --password='{$vals['db_pass']}' "
. "-h {$vals['db_host']} -D {$vals['db_name']} < {$script_path}";
$output = shell_exec($command . '/shellexec.sql');
See also my answers to these related questions:
- Loading .sql files from within PHP
- is it possible to call a sql script from a stored procedure in another sql script?
- PHP: multiple SQL queries in one mysql_query statement
Loading .sql files from within PHP
I'm getting the feeling that everyone here who's answered this question doesn't know what it's like to be a web application developer who allows people to install the application on their own servers. Shared hosting, especially, doesn't allow you to use SQL like the "LOAD DATA" query mentioned previously. Most shared hosts also don't allow you to use shell_exec.
Now, to answer the OP, your best bet is to just build out a PHP file that contains your queries in a variable and can just run them. If you're determined to parse .sql files, you should look into phpMyAdmin and get some ideas for getting data out of .sql files that way. Look around at other web applications that have installers and you'll see that, rather than use .sql files for their queries, they just package them up in PHP files and just run each string through mysql_query or whatever it is that they need to do.
Execute a *.sql file using php
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.
PDO run script from .sql file
Since this is a script you can execute SQL with the following code:
try {
$mysql_host = "localhost";
$mysql_database = "planets_test";
$mysql_user = "root";
$mysql_password = "";
$dbh= new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_user, $mysql_password);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$query = file_get_contents("planets_db.sql");
$dbh->exec($query);
print("Created $table Table.\n");
} catch(PDOException $e) {
echo $e->getMessage();//Remove or change message in production code
}
If there is an error it should tell you what went wrong.
.sql file execution using php and pdo
After using this script my mysql.sql
file imported successfully.
Thanks to Oshane Bailey
How to run a set of SQL queries from a file, in PHP?
- Don't use
file()
unless you really want the data line-by-line.file_get_contents()
is better. $query==file($file_name);
I don't think you want to do a comparison here.mysql_query
will only ever execute a single query at once. You'll have to come up with some way of separating your queries in the file and run them one-by-one.
Related Topics
Replacing Accented Characters PHP
What's the Difference Between :: (Double Colon) and -≫ (Arrow) in PHP
Download File Through an Ajax Call PHP
PHP: Convert Any String to Utf-8 Without Knowing the Original Character Set, or At Least Try
Convert Flat Array to a Delimited String to Be Saved in the Database
How to Find Entry by Object Property from an Array of Objects
Remove Excess Whitespace from Within a String
Explode PHP String by New Line
How to Enable Curl in PHP/Xampp
How to Execute Two MySQL Queries as One in PHP/MySQL
How to Find Out If You'Re Using Https Without $_Server['Https']
Performance of For VS Foreach in PHP
Getting Checkbox Values on Submit