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.
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
2. Next create a database and also connect to the database with the below code
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
4. Creating the php code with the code given below and save as index.php to end with the tutorial
Following the steps successfully you have known how to import sql to the 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.
Get our updates delivered to your inbox
Very good article! We are linking to this particularly great content on our site.
ReplyDeleteKeep up the good writing.
What's up Dear, are you actually visiting this site regularly,
ReplyDeleteif so after that you will definitely obtain good know-how.
Hello would you mind letting me know which web host you're
ReplyDeleteworking 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!
Hello are using Wordpress for your blog platform? I'm new to the
ReplyDeleteblog 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!
Hi, I read your blogs like every week. Your humoristic style is awesome, keep doing what you're doing!
ReplyDeleteSpot on with this write-up, I seriously believe that this amazing site needs far more attention. I'll
ReplyDeleteprobably be returning to see more, thanks for the advice!
Greetings! Quick question that's totally off topic. Do you
ReplyDeleteknow 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!
I just like the valuable information you provide for your articles.
ReplyDeleteI'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!
Peculiar article, totally what I was looking for.
ReplyDeleteThanks very nice blog!
ReplyDeleteI don't even understand how I finished up here, however I believed this put up used
ReplyDeleteto 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!
I am genuinely thankful to the holder of this website who has shared this
ReplyDeletegreat article at at this place.
This is my first time pay a quick visit at here and i am really impressed to read all at one place.
ReplyDeleteYes! Finally something about minecraft.
ReplyDeleteHello, just wanted to tell you, I loved this post.
ReplyDeleteIt was helpful. Keep on posting!
I have been browsing online more than 3 hours today,
ReplyDeleteyet 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.
Thank you for every other fantastic article. The place else may anybody get that
ReplyDeletekind of info in such an ideal means of writing? I have
a presentation subsequent week, and I am on the search
for such info.
Good information. Lucky me I ran across your site by
ReplyDeleteaccident (stumbleupon). I have saved as a favorite for
later!
I savour, lead to I discovered exactly what I used to be looking for.
ReplyDeleteYou've ended my four day lengthy hunt! God Bless you man. Have
a great day. Bye