PHP Import SQL from a .sql file

David Carr

Development Tools

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);

 

Read articles directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Copyright © 2006 - 2024 DC Blog - All rights reserved.