Thursday 23 May 2013

Excel To Mysql using PHP through CSV file format.


PRELIMES:                 METHOD:1
--------
Excel to csv convertion . create table in mysql
----------

csv to mysql table data import:  100% succcess.  
------------------------------- --------------->>

<?php
$sql=mysql_connect("localhost","root","");   //hostname,username,password
 mysql_select_db("test",$sql); // employee is the database name.
 $path = "list.csv"; // csv file in the same directory
  if (($handle = fopen($path, "r")) !== FALSE)
 {
$i=1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
 {
echo $data[0],$data[1];
    mysql_query("insert into t1(name,mailid) values('".$data[0]."','".$data[1]."')");//t1->table name and its files names

echo $i;$i++;
echo "<br >";

  }
     fclose($handle);
}
?>

------------------------------------------------------------------------------


METHOD : 2
===========
working : text file to mysql data insert
LOAD DATA INFILE 'c:/wamp/www/touch/text.txt' INTO TABLE t1 FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES(name,email)
 Excel->csv,txt to mysql table data import 90% scuccess.
=================================================>
PRELIMES:
--------
Excel to csv convertion . create table in mysql
---------------------------------

Short and sweet solution for excel to mysql data import:
Working good for txt file formats. IN DETAIL:
tbl name=t1 feilds are= name varchar,email varchar;
text.txt file <<== this text file first lines table column names:
name, email "n1", "e1" "n2", "e2" "n3", "e3" "n4", "e4" "n5", "e5" "n6", "e6" "n7", "e7"
SQL query in wamp

LOAD DATA INFILE 'c:/wamp/www/touch/text.txt' INTO TABLE t1 FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES(name,email)

For this commnad run successfully we have create folders for separately.

Real one is

C:\wamp\mysql\data\wamp\www\touch\text.txt <<==pysical file path is.

But we mention c:/wamp/touch/text.txt
------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment