In the previous post you see how to Import an Excel file in Mysql database . Now In this tutorial, we will see how to Export or Download Mysql table in Excel Sheet form.
The CSV stands for comma separated values. You often use the CSV file format to exchange data between applications such as Microsoft Excel, Google Docs, etc.
Import an Excel file in MySql Database using php
welcome back to shortlearner.com, In this tutorial, we will see how to Import an Excel file in Mysql database. In the First step we will create a Database to insert an excel data into Mysql table, a form to make a view to upload an Excel Sheet and also create a PHP script to perform operation to insert data into Database.
Create Database
To Insert excel sheet data into MySql database, we need to create a database named as ‘test_excel’ and also create a table named as ‘excel_sheet’
Also Read :
How to Install PHP on CentOS.
How to integrate Razorpay Payment Gateway using PHP.
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | -- phpMyAdmin SQL Dump -- version 4.7.0 -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 -- Generation Time: Dec 13, 2017 at 01:51 PM -- Server version: 10.1.25-MariaDB -- PHP Version: 5.6.31 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `test_excel` -- -- -------------------------------------------------------- -- -- Table structure for table `excel_sheet` -- CREATE TABLE `excel_sheet` ( `website` varchar(255) NOT NULL, `category` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- |
Config.php
after creating database , we move on the config.php page.. here we make a database connection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php function getdb(){ $servername = "localhost"; $username = "root"; $password = ""; $db = "test_excel"; try { $conn = mysqli_connect($servername, $username, $password, $db); //echo "Connected successfully"; } catch(exception $e) { echo "Connection failed: " . $e->getMessage(); } return $conn; } ?> |
Index.php
after creating database , we move on the index page.. in the index page we take a csv file from our computer and redirect it to functions.php page . where we check that the file which is selected is CSV or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script> </head> <body> <div id="wrap"> <div class="container"> <div class="row"> <form class="form-horizontal" action="functions.php" method="post" name="upload_excel" enctype="multipart/form-data"> <fieldset> <!-- Form Name --> <legend>Form Name</legend> <!-- File Button --> <div class="form-group"> <label class="col-md-4 control-label" for="filebutton">Select File</label> <div class="col-md-4"> <input type="file" name="file" id="file" class="input-large"> </div> </div> <!-- Button --> <div class="form-group"> <label class="col-md-4 control-label" for="singlebutton">Import data</label> <div class="col-md-4"> <button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Import</button> </div> </div> </fieldset> </form> </div> </div> </div> </body> </html> |
functions.php
In this page we catch the file and check it , if our file is csv than it import into database otherwise it shows the error and redirect to index.php page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php if(isset($_POST["Import"])){ $filename=$_FILES["file"]["tmp_name"]; if($_FILES["file"]["size"] > 0) { $file = fopen($filename, "r"); while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) { echo $sql = "INSERT into excel_sheet (website, category) values ('".$getData[0]."','".$getData[1]."')"; $result = mysqli_query($conn, $sql); if(!isset($result)) { echo "<script type=\"text/javascript\"> alert(\"Invalid File:Please Upload CSV File.\"); window.location = \"index.php\" </script>"; } else { echo "<script type=\"text/javascript\"> alert(\"CSV File has been successfully Imported.\"); window.location = \"index.php\" </script>"; } } fclose($file); } } ?><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-7265442612378874" data-ad-slot="1524940440"></ins> |
Keep Learning..
Improve your website Ranking with Social Media SEO meta Tags
Welcome back to Shortlearner.com, todays post is about meta tags.
Meta Tags are one of the most basic elements of SEO(search engine optimization). The search engines reads the meta tags to get a summarized idea of what your site is about and what exactly your keywords are.Meta tags list your site in Top.Meta tags are located inside your html’s head area.
Important attributes of meta tag:
Host a website on free server
If you have designed your website and now you want it to be uploaded on server(you want hosting of your website) then 000webhost is a best solution .It provides free domain and hosting with very easy process.Follow below steps:-
Step 1:
In address bar type 000webhost sign up. and then click on the first link.
How to make package in java ?
Today we will learn how to make package in java, but before making java package , first of all we should know about what is java package ,and why it needs.? Basically package are like folders which contain java class files.package are made when we want to use some set of code or values in another program.it decrease the size of program.when we make any project we make package so the program become readable and usable.when we make package we can import it any program.