php script to convert csv file into mysql
09-25-09, 01:17 AM
Wannabe Coder
Join Date: Aug 2009
Posts: 117
Thanks: 15
Thanked 0 Times in 0 Posts
php script to convert csv file into mysql
PHP Code:
<?php
$databasehost = "localhost" ;
$databasename = "test" ;
$databasetable = "sample" ;
$databaseusername = "" ;
$databasepassword = "" ;
$fieldseparator = "," ;
$lineseparator = "\n" ;
$csvfile = "Tasks.csv" ;
$addauto = 1 ;
$save = 1 ;
$outputfile = "output.sql" ;
/********************************/
if(! file_exists ( $csvfile )) {
echo "File not found. Make sure you specified the correct path.\n" ;
exit;
}
$file = fopen ( $csvfile , "r" );
if(! $file ) {
echo "Error opening data file.\n" ;
exit;
}
$size = filesize ( $csvfile );
if(! $size ) {
echo "File is empty.\n" ;
exit;
}
$csvcontent = fread ( $file , $size );
fclose ( $file );
include( "db.php" );
$lines = 0 ;
$queries = "" ;
$linearray = array();
foreach( split ( $lineseparator , $csvcontent ) as $line ) {
$lines ++;
$line = trim ( $line , " \t" );
$line = str_replace ( "\r" , "" , $line );
$linearray = explode ( $fieldseparator , $line );
$linemysql = implode ( "','" , $linearray );
if( $addauto )
$query = "insert into $databasetable values('',' $linemysql ');" ;
else
$query = "insert into $databasetable values(' $linemysql ');" ;
$queries .= $query . "\n" ;
@ mysql_query ( $query );
}
@ mysql_close ( $con );
if( $save ) {
if(! is_writable ( $outputfile )) {
echo "File is not writable, check permissions.\n" ;
}
else {
$file2 = fopen ( $outputfile , "w" );
if(! $file2 ) {
echo "Error writing to the output file.\n" ;
}
else {
fwrite ( $file2 , $queries );
fclose ( $file2 );
}
}
}
echo "Found a total of $lines records in this csv file.\n" ;
?>
--------------------------------------------------------------------------
i am able to import csv file by this script
my problem is, in my csv file there r arround 97 records. among them from 60-90 some records are having data and time(23 Sep, 2009 09:00:00). 30 records containing data and time are not inserted in mysql database.
if anyone knows help me out??????????????????????????
Last edited by wirehopper; 09-25-09 at 07:25 AM .
Reason: PHP tags
09-25-09, 02:42 AM
Coding Addict
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
Here is a working script you can have, I wrote it some time ago for a project it works well.
You can modify it to suit your needs.
PHP Code:
<?PHP mysql_connect ( "localhost" , "Database" , "Pasword" ) or die( mysql_error ()); mysql_select_db ( "Table" ) or die( mysql_error ()); $file_handle = fopen ( "MD.csv" , "r" ); while (! feof ( $file_handle ) ) { $line_of_text = fgetcsv ( $file_handle , 1024 ); print $line_of_text [ 0 ] . "<BR>" ; $Name = $line_of_text [ 0 ]; $Street = $line_of_text [ 1 ]; $City = $line_of_text [ 2 ]; $State = $line_of_text [ 3 ]; $Zipcode = $line_of_text [ 4 ]; $Phone = $line_of_text [ 8 ]; mysql_query ( "INSERT INTO Business (Name,Street,City,State,Zipcode,Phone) VALUES(' $Name ',' $Street ',' $City ',' $State ',' $Zipcode ',' $Phone ') " ); } fclose ( $file_handle ); ?>
__________________
This post was created with 100% recycled electrons.
09-25-09, 02:43 AM
Coding Addict
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
The basic difference is your trying to separate the string yourself, this is overcomplicated, you can just use the fgetcsv function to have PHP separate the string for you and store the data in an array.
__________________
This post was created with 100% recycled electrons.
09-25-09, 05:13 AM
Wannabe Coder
Join Date: Aug 2009
Posts: 117
Thanks: 15
Thanked 0 Times in 0 Posts
in this also same problem , rows that contain date and time(27 sep, 9:00) are not imported into database
09-25-09, 07:25 AM
-
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Reformat the date and time to match MySQL.
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off