PDO import sql from a .sql file


Using composer include the repository by typing the following into a terminal

composer require dcblogdev/sql-import


Include the composer autoloader, import the Import namespace.

Define your database file path and credentials, the option dropTables, when set to true, will delete all the tables in the database before the sql file gets imported.

The option forceDropTables is optional and not recommended. When enabled together with dropTables, the deletion of tables will be executed with disabled foreign key checks. Only use this method if you are sure that the integrity of the existing data in the database does not matter.

use Dcblogdev\SqlImport\Import;

$filename = 'database.sql';
$username = 'root';
$password = '';
$database = 'sampleproject';
$host = 'dev';
$dropTables = true;
$forceDropTables = false;

new Import($filename, $username, $password, $database, $host, $dropTables, $forceDropTables);

Help support the blog so that I can continue creating new content!

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.