How To Import Mysql File Into Database Using Php
Below are the content of this tutorials:
• we require to practise a mode for the html using css.
• we require to practise a database in addition to every bit good connect to the database banking firm fit hither for how to practise database.
• We require our sql script that nosotros require to import into the database.
• We are require to write the php script that volition assist inward the execution process.
We are going to live taking each of the content measuring yesteryear step,so follow the steps carefully in addition to import your script to your database in addition to thus cheaply inside shortest menses of time
1. Open your code editor re-create in addition to glue the below code in addition to salvage every bit 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 practise a database in addition to every bit good connect to the database amongst the below code
<?php $db['db_host'] = "localhost"; $db['db_user'] = "root"; $db['db_pass'] =""; $db['db_name'] = "sqldatabase"; foreach($db every bit $key => $value){ define(strtoupper($key),$value); } $connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME); ?>
Save the higher upwards code amongst db.php
3. Now the sample SQL nosotros are going to live import are given below in addition to it salvage amongst 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` appointment NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
4. Creating the php code amongst the code given below in addition to salvage every bit index.php to destination amongst 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 every bit $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 live executed due to around 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 lot bring known how to import sql to the database using php.