How To Import Mysql File Into Database Using Php

FAST DOWNLOADads
Download
In today tutorial we are going to be looking at how to import an sql file into the database to create table in the database for storage of database of personal details.You can modify for your own taste,this tutorial is basically for learning.But before we proceed there are some basic things we need to set in place to run this code successfully.
In today tutorial we are going to be looking at how to import an sql file into the databas How To Import MySQL File Into Database Using PHP

Below are the content of this tutorials:

we need to create a style for the html using css.
we need to create a database and also connect to the database check here for how to create database.
We need our sql script that we need to import into the database.
We are require to write the php script that will help in the execution process.
We are going to be taking each of the content step by step,so follow the steps carefully and import your script to your database so cheaply within shortest period of time

1. Open your code editor copy and paste the below code and save as style.css
<style>   body {   width:600px;   text-align:center;   }   .import-message {    padding: 10px;   }   .success-message {    background-color: #a8ebc4;       border-color: #1b7943;       color: #1b7943;   }   .error-message {    border-color: #d96557;       background: #f0c4bf;       color: #d96557;   }  </style>

2. Next create a database and also connect to the database with the below code

<?php  $db['db_host'] = "localhost";  $db['db_user'] = "root";  $db['db_pass'] ="";  $db['db_name']  = "sqldatabase";    foreach($db as $key => $value){           define(strtoupper($key),$value);  }            $connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);  ?>


Save the above code with db.php

3. Now the sample SQL we are going to be import are given below and it save with mydatabase.sql

CREATE TABLE IF NOT EXISTS `contact_table` (    `id` int(11) NOT NULL,    `first_name` varchar(255) NOT NULL,    `last_name` varchar(255) NOT NULL,    `address` varchar(255) NOT NULL,    `email` varchar(255) NOT NULL,    `phone` varchar(20) NOT NULL,    `date_of_birth` date NOT NULL  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


4. Creating the php code with the code given below and save as index.php to end with the tutorial

<html>  <head>  <link rel="stylesheet" type="text/css" href="style.css">  </head>  <body>  <?php  <?php include "db.php";?>  $query = ' ';  $myScript = file('mydatabase.sql');  foreach ($myScript as $line) {      $startWith = substr(trim($line), 0 ,2);   $endWith = substr(trim($line), -1 ,1);      if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {    continue;   }       $query = $query . $line;   if ($endWith == ';') {    mysqli_query($connection,$query) or die('<div class="error-message import-message">There SQL Query cannot be executed due to some problems <b>' . $query. '</b></div>');    $query= ' ';     }  }  echo '<div class="success-message import-message">importation of the SQL file was  successfully</div>';  ?>  </body>  </html>


Following the steps successfully you have known how to import sql to the database using php.
FAST DOWNLOADads
| Server1 | Server2 | Server3 |
Download
Next Post Previous Post