Use PHP to dynamically create a database from a .sql file
When creating application installation scripts, it is often necessary to dynamically create a database in a PHP environment. While the initial database creation is easy to handle, loading multiple .sql files becomes more complex.
To solve this problem, you can use a method similar to the phpMyAdmin import command in PHP. The specific steps are as follows:
$db = new PDO($dsn, $user, $password);
$sql = file_get_contents('file.sql');
exec()
method: $qr = $db->exec($sql);
This process will effectively load the .sql file into the database, create all tables and populate them with the specified data.
The above is the detailed content of How Can I Use PHP to Dynamically Create Databases from .sql Files?. For more information, please follow other related articles on the PHP Chinese website!