Tools | Development

David Carr

PHP Import SQL from a .sql file

I've released a new package to easily import a .sql file into a database using PDO.

To install the package import it using composer:

composer require daveismyname/sql-import

Or download and include import.php manually from https://github.com/daveismyname/sql-import

Then to use the library, define your database file path and credentials, the last option dropTables when set to true will delete all the tables in the database before import the sql file.

<?php
require('vendor/autoload.php');

use Daveismyname\SqlImport\Import;

$filename = 'database.sql';
$username = 'root';
$password = '';
$database = 'sampleproject';
$host = 'localhost';
$dropTables = true;
new Import($filename, $username, $password, $database, $host, $dropTables);

 

Support my work by donating with PayPal.

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2020 DC Blog. All code MIT license. All rights reserved.