Home >Database >Mysql Tutorial >How Can I Use PHP to Dynamically Create Databases from .sql Files?
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:
<code class="language-php">$db = new PDO($dsn, $user, $password);</code>
<code class="language-php">$sql = file_get_contents('file.sql');</code>
exec()
method: <code class="language-php">$qr = $db->exec($sql);</code>
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!