Current location: Hot Scripts Forums » Programming Languages » PHP » php script to convert csv file into mysql


php script to convert csv file into mysql

Reply
  #1 (permalink)  
Old 09-25-09, 01:17 AM
hemi hemi is offline
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-25-09, 02:42 AM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
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_handle1024);

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-25-09, 02:43 AM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-25-09, 05:13 AM
hemi hemi is offline
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-25-09, 07:25 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Reformat the date and time to match MySQL.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code 2 convert .doc file into .pdf file jeephp PHP 7 11-03-10 05:31 AM
Need Help Creating a PHP script to edit html file Swag PHP 5 07-22-09 10:38 PM
[SOLVED] it works on localhost but not on hosting account? myslowquietlife PHP 42 12-19-08 10:31 AM
Need Advanced PHP MySql Search Script HowdeeDoodee Script Requests 0 05-08-07 01:46 AM
Flat File PHP News Script bosox_fan Script Requests 1 11-09-05 03:49 PM


All times are GMT -5. The time now is 12:34 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.