How to Import SQL File in Mysql Database using PHP

Webslesson

PHP, MySql, Jquery, AngularJS, Ajax, Codeigniter, Laravel Tutorial

Friday, 23 June 2017

How to Import SQL File in Mysql Database using PHP

Import of Sql table by using PHP script, it will be helpful when we want to make database dynamically when execution of script. For example When we have developed any App or any type of Web application plugin then at that time when We have installed App or plugin online then at that time importing of SQL table will be helpful to installed application table into database at the of set up of App or plugin.

In this post, we will going to learn how to import SQL script into a Mysql Database by using PHP script. Here we will seen on SQL table file in which we have on images table for import into Database. PHP script will read SQL file line by line and make query statement and then after it will execute query statement by using PHP Mysql query function.

Here we have make simple PHP script is used for import SQL table file into Mysql database. It will convert content of file into PHP array by using PHP built in file() function and this PHP Array data has been fetch by foreach loop and make query by removing comment and empty line from script.

In PHP script we have make condition to check if it has found any symbols that used from define comment then it will not check code and it will check only script without comment symbol. By using this PHP script you can easily import sql file into Msyql database.

Source Code

index.php

  $output = ''; > > > if($count > 0) < $message = ''; > else < $message = ''; > > else < $message = ''; > > else < $message = ''; > > ?>       

How to Import SQL File in Mysql Database using PHP



Источник

How to Import MySQL Database from SQL File using PHP

Importing SQL script via programming will be useful when we need to create database structure dynamically. For example, if we provide APP or plugin to download and install from online, the dynamic SQL import will be used to setup the application database at the time of installing the APP or plugin.

To import SQL file in the database, you have to login to your hosting server or phpMyAdmin. Also, you can import the database from PHP script without login to your hosting server or phpMyAdmin.

Import database from PHP script is extremely helpful when you need to enable the client to import the database from your web application. A backup of the database should be taken for importing tables in MySQL database.

In this tutorial, we are going to import a SQL file into a database using PHP programming. In this example, we have a file containing SQL for creating contact table and inserting data. The PHP code parses the file line by line and extracts the query statements to execute using PHP MySQL function.

importDatabaseTables() work in PHP, you can import tables in the database from a .sql file. The following given parameters are required to import or rstore MySQL database using PHP.

  • $dbHost – Required. Specifies the host name of the database.
  • $dbUname – Required. Specifies the database username.
  • $dbPass – Required. Specifies the database password.
  • $dbName – Required. Specifies the database name in which you wants to import.
  • $filePath – Required. Specifies the path of the SQL file from where the tables will be imported.

Источник

How To Import SQL File With PHP

This post was first published on How To Import SQL File With PHP Sometimes there are times when you can not use PhpMyAdmin or any database program, and you have to use other options to import the Sql file into your database. Here, I’ll tell you how you can import with both the old mysql_query and the new mysqli_query in Php. You can also access the codes from the my Github project. You need to do followings on both methods; upload Sql file and php file at the same place and run the php file through the site address. You can also run console commands through the server. Update $filename with the name of sql. Update the $mysql_host variable with the database server. If the database server is not different, you can leave it as it is. $mysql_username and $mysql_password are the username and password for your database. $mysql_database is the database name, update it with your database name.

With mysql_query Function

 // Name of the file $filename = 'sql.sql'; // MySQL host $mysql_host = 'localhost'; // MySQL username $mysql_username = 'username'; // MySQL password $mysql_password = 'password'; // Database name $mysql_database = 'database'; // 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 \'' . $templine . '\': ' . mysql_error() . '

'
); // Reset temp variable to empty $templine = ''; > > echo "Tables imported successfully";

With mysqli_query class

 // Name of the file $filename = 'sql.sql'; // MySQL host $mysql_host = 'localhost'; // MySQL username $mysql_username = 'username'; // MySQL password $mysql_password = 'password'; // Database name $mysql_database = 'database'; // Connect to MySQL server $con = @new mysqli($mysql_host,$mysql_username,$mysql_password,$mysql_database); // Check connection if ($con->connect_errno)  echo "Failed to connect to MySQL: " . $con->connect_errno; echo "
Error: "
. $con->connect_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 $con->query($templine) or print('Error performing query \'' . $templine . '\': ' . $con->error() . '

'
); // Reset temp variable to empty $templine = ''; > > echo "Tables imported successfully"; $con->close($con);

Источник

How to Import SQL into a Database using PHP

Importing SQL script via programming will be useful when we need to create database structure dynamically. For example, if we provide APP or plugin to download and install from online, the dynamic SQL import will be used to setup the application database at the time of installing the APP or plugin.

In this tutorial, we are going to import a SQL file into a database using PHP programming. In this example, we have a file containing SQL for creating contact table and inserting data. The PHP code parses the file line by line and extracts the query statements to execute using PHP MySQL function.

PHP Code to Import SQL

This simple PHP code is used to import SQL file into a database. It extracts the file contents into an array of lines by using PHP file(). The line array is iterated in a foreach loop to construct the query by omitting comment and empty lines.

The condition checks if the line contains symbols used as comment line delimiters like (–, /*, //). If so, the loop skips the current iteration and continue with the next iteration.

In this way, it repeats the same until the ‘end of query’ statement. Once, the end of query character is found then the query will be executed. Then the query variable will be reset before starting next iteration.

 $query = $query . $line; if ($endWith == ';') < mysqli_query($conn,$query) or die('
Problem in executing the SQL query ' . $query. '
'); $query= ''; > > echo '
SQL file imported successfully
'; ?>

Источник

How to import sql file in MySQL database using PHP?

We usually use phpmyadmin for importing a large sql file to our database. There are tons of other options are there to do this. Most of the people use the phpMyAdmin tool for their database maintenance operations. But most of the host you have to login to cpanel and then navigate to the phpMyAdmin from there.

In this tutorial we are demonstrating how to make our own application that import a .sql file using PHP script. This little php script helps you to restore our MySQL database with very few steps.

How to import sql file in MySQL database using PHP?

The following details needed in hand to perform the import action using PHP.

  • $filename – Required. The filename of dumped database.
  • $mysqlHost– Required. The mysql Host name
  • $mysqlUser – Required. The mysql server username.
  • $mysqlPassword – Required. The MySql user password. Often this can be empty for your localhost mysql installaiton. But it’s a must when you go in the production server.
  • $mysqlDatabase – The selected database that you need to import data.
 > echo "Tables imported successfully"; ?>

Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

Reader Interactions

Comments

the line
mysqli_query($tempLine) or print(“Error in ” . $tempLine .”:”. mysqli_error());
must be
mysqli_query($link, $tempLine) or print(“Error in ” . $tempLine .”:”. mysqli_error());
then it works well

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Читайте также:  Deleting all variables in python
Оцените статью