Connection - PHP: (You May Also Read)
Connection - PHP: (You May Also Read)
Database using PHP. When we develop some database linked website where
we have to enter data in database, sometime we have to upload bulk data in
database. If we insert data one by one it will be very difficult and time
consuming. Here You will learn how to insert bulk of data in database from
excel sheet in a couple of minutes.
This script will import data from .csv file, so save your excel sheet in .csv file.
The code is very simple we have two file here
1. index.php
2. connection.php
connection.php contain our MySQL connection detail that is localhost,
username, password and obviously database name.
1 $hostname = "localhost";
2 $username = "root";
3 $password = "";
4 $database = "test";
5
6
7 $conn = mysql_connect("$hostname","$username","$password") or die(mysql_error());
8 mysql_select_db("$database", $conn);
index.php, it contain simple form to let user to upload file and some PHP
code to insert that data in our MySQL database. So lets have a view on our
index.php file
(You may also read: Export MySQL Table Data into Excel Sheet Format in
PHP)
HTML form:
OK there is one question that how we can get the column address of excel
sheet or .csv file. We are getting our data in form of an array. Then obviously
we should have to treat it like an array (if you don’t know about array please
read it at php.net). So it will work like this.
1 $filesop[number_of_column];
$filesop is our array name and number _of_column refers to our address
of column of excel sheet. And array always start from 0 it means that the
address of 1st column of excel sheet is 0 here.
////
22 <form action="upload.php">
upload.php:
<?php
if(isset($_POST["Import"]))
{
$host="localhost"; // Host name.
$db_user="root";
$db_password="";
$db='test'; // Database name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-
strrpos($filename,".")));
if($_FILES["file"]["size"] > 0)
{
}
?>
333.Hello all,
I have found my solution. Now allz working perfectly fine. I am posting my full code.
upload.php:
<html>
<body style="
background-color: rgb(128, 151, 185);
">
</body>
</html>
import_file.php:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
//echo "Stored in: " . $_FILES["file"]["tmp_name"];
$a=$_FILES["file"]["tmp_name"];
//echo $a;
$connect = mysql_connect('localhost','root','');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
//your database name
$cid =mysql_select_db('test',$connect);
$col1 = $slice[0];
$col2 = $slice[1];
$col3 = $slice[2];
$col4 = $slice[3];
33Dear,
I need help, how to import my Excel file into Mysql Database. I can successfully
upload excel file but its not correctly formated uploaded or imported. please
check this image,
1. <?php
2. if(isset($_POST["Import"]))
3. {
4. //First we need to make a connection with the database
5. $host='localhost'; // Host Name.
6. $db_user= 'root'; //User Name
7. $db_password= '';
8. $db= 'product_record'; // Database Name.
9.
10. $conn=mysql_connect($host,$db_user,$db_password) or die
(mysql_error());
11. mysql_select_db($db) or die (mysql_error());
12.
13. echo $filename=$_FILES["file"]["tmp_name"];
14. if($_FILES["file"]["size"] > 0)
15. {
16.
17. $file = fopen($filename, "r");
18. //$sql_data = "SELECT * FROM prod_list_1 ";
19. while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
20.
21. {
22. //print_r($emapData);
23. //exit();
24.
25. $sql = "INSERT into prod_list_1(p_bench,p_name,p_price,p_reason)
values ('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]')";
26. mysql_query($sql);
27. }
28. fclose($file);
29. echo 'CSV File has been successfully Imported';
30. header('Location: index.php');
31. }
32. else
33. echo 'Invalid File:Please Upload CSV File';
34.
35. }
36. ?>
44database Details:
database name => phpgang
table name => excel
db.sql
Database file run in your MySQL to create database and add data in table.
db.php
1 <?php
2 define('DB_SERVER', 'localhost');
3 define('DB_USERNAME', 'username');
4 define('DB_PASSWORD', 'password');
5 define('DB_DATABASE', 'database');
6 $connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
7 ?>
index.php
Contains HTML and PHP include library and insert records in database.
PHP
1 <?php
2
3 ini_set("display_errors",1);
4 require_once 'excel_reader2.php';
5 require_once 'db.php';
6
7 $data = new Spreadsheet_Excel_Reader("example.xls");
8
9 echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";
1
0 $html="<table border='1'>";
1 for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
1 {
1 if(count($data->sheets[$i][cells])>0) // checking sheet not empty
2 {
1 echo "Sheet $i:<br /><br />Total rows in sheet $i ".count($data->sheets[$i]
3 [cells])."<br />";
1 for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of
4 the sheet
1 {
5 $html.="<tr>";
1 for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is
6 created to get data in a table format.
1 {
7 $html.="<td>";
1 $html.=$data->sheets[$i][cells][$j][$k];
8 $html.="</td>";
1 }
9 $eid = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j]
2 [1]);
0 $name = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j]
2 [2]);
1 $email = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j]
2 [3]);
2 $dob = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j]
2 [4]);
3 $query = "insert into excel(eid,name,email,dob) values('".$eid."','".
2 $name."','".$email."','".$dob."')";
4
2 mysqli_query($connection,$query);
5 $html.="</tr>";
2 }
6 }
2
7 }
2
8 $html.="</table>";
2 echo $html;
9 echo "<br />Data Inserted in dababase";
3 ?>
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
66 <?php
require 'Classes/PHPExcel/IOFactory.php';
// Mysql database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "databasename";
$inputfilename = 'example_file.xlsx';
$exceldata = array();
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
$exceldata[] = $rowData[0];
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
?>