How to Import a .SQL File in MySQL Database Using PHP

How do I import a .sql file in mysql database using PHP?


Warning: mysql_* extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

Whenever possible, importing a file to MySQL should be delegated to MySQL client.

I have got another way to do this, try this

<?php

// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dump';

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>

This is working for me

how to import .sql file in mysql databse with php

Try this code. You have to read the sql file first and than execute:

eg:

$sqlSource = file_get_contents('insert.sql');

mysqli_multi_query($sql,$sqlSource);

how to import a .sql file using php function

It's working now!
Firstly, it didn't work in local server and secondly, instead of exec() function I used the system() function and finally, it generates a table in my database. Thanks everyone!

How do I import an SQL file using the command line in MySQL?

Try:

mysql -u username -p database_name < file.sql

Check MySQL Options.

Note 1: It is better to use the full path of the SQL file file.sql.

Note 2: Use -R and --triggers to keep the routines and triggers of original database. They are not copied by default.

Note 3 You may have to create the (empty) database from MySQL if it doesn't exist already and the exported SQL don't contain CREATE DATABASE (exported with --no-create-db or -n option), before you can import it.

importing an .sql.gz file into mysql using php

This is how i fixed it:

$lines = gzfile($fileName);

<?php

class reader{

public $dbName;
public $fileName;

public function import($dbName, $fileName){
$serverName = "localhost";
$userName = "root";
$password = "";

$conn = new mysqli($serverName,$userName,$password,$dbName);

$temp = "";
$lines = gzfile($fileName);

foreach($lines as $key => $line ){
if(substr($line, 0, 2) == '--' || $line == '') continue;

$temp .= $line;

if(substr(trim($line), -1, 1) == ';'){
$conn->query($temp) or print('Error performing query \'<strong>' . $temp . '\': '. '<br /><br />');
$temp = "";}

}
echo "<script>alert('Database Imported')</script>";
}
}
$bestand = new reader();
# dbname | file name
$bestand->import("test", "testdb.sql.gz");

?>

How to import SQL file to MySQL database without phpMyAdmin

Sure, it's very easy. On the command line...

mysql -u<USERNAME> -p<PASSWORD> <DATABASE> < /path/to/my.sql

For example:

mysql -urjdown -pmypass mydatabase < /home/rjdown/my.sql


Related Topics



Leave a reply



Submit