Breaking

Tuesday 23 January 2018

How To Import MySQL File Into Database Using PHP

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.
how-to-import-mysql-using-php.jpg

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.
   

Get our updates delivered to your inbox



19 comments:

  1. Very good article! We are linking to this particularly great content on our site.
    Keep up the good writing.

    ReplyDelete
  2. What's up Dear, are you actually visiting this site regularly,
    if so after that you will definitely obtain good know-how.

    ReplyDelete
  3. Hello would you mind letting me know which web host you're
    working with? I've loaded your blog in 3 completely different web browsers and I
    must say this blog loads a lot faster then most.
    Can you recommend a good hosting provider at a reasonable price?
    Thank you, I appreciate it!

    ReplyDelete
  4. Hello are using Wordpress for your blog platform? I'm new to the
    blog world but I'm trying to get started and set up my own.
    Do you need any coding knowledge to make your own blog?
    Any help would be greatly appreciated!

    ReplyDelete
  5. Hi, I read your blogs like every week. Your humoristic style is awesome, keep doing what you're doing!

    ReplyDelete
  6. Spot on with this write-up, I seriously believe that this amazing site needs far more attention. I'll
    probably be returning to see more, thanks for the advice!

    ReplyDelete
  7. Greetings! Quick question that's totally off topic. Do you
    know how to make your site mobile friendly? My web site looks weird when browsing
    from my iphone4. I'm trying to find a template or plugin that might be
    able to fix this issue. If you have any suggestions, please share.
    Appreciate it!

    ReplyDelete
  8. I just like the valuable information you provide for your articles.

    I'll bookmark your blog and check once more right here regularly.
    I'm reasonably certain I will be informed many new stuff
    proper here! Best of luck for the following!

    ReplyDelete
  9. Peculiar article, totally what I was looking for.

    ReplyDelete
  10. Thanks very nice blog!

    ReplyDelete
  11. I don't even understand how I finished up here, however I believed this put up used
    to be great. I do not recognize who you are however
    certainly you are going to a well-known blogger
    if you are not already. Cheers!

    ReplyDelete
  12. I am genuinely thankful to the holder of this website who has shared this
    great article at at this place.

    ReplyDelete
  13. This is my first time pay a quick visit at here and i am really impressed to read all at one place.

    ReplyDelete
  14. Yes! Finally something about minecraft.

    ReplyDelete
  15. Hello, just wanted to tell you, I loved this post.

    It was helpful. Keep on posting!

    ReplyDelete
  16. I have been browsing online more than 3 hours today,
    yet I by no means discovered any interesting article like yours.
    It is beautiful worth enough for me. In my opinion, if all web owners
    and bloggers made just right content as you probably did, the web shall be
    much more helpful than ever before.

    ReplyDelete
  17. Thank you for every other fantastic article. The place else may anybody get that
    kind of info in such an ideal means of writing? I have
    a presentation subsequent week, and I am on the search
    for such info.

    ReplyDelete
  18. Good information. Lucky me I ran across your site by
    accident (stumbleupon). I have saved as a favorite for
    later!

    ReplyDelete
  19. I savour, lead to I discovered exactly what I used to be looking for.
    You've ended my four day lengthy hunt! God Bless you man. Have
    a great day. Bye

    ReplyDelete

Want to get all latest updates from DbencoPlanet.com?

Subscribe to DbencoPlanet to get all the latest updates free!