Jump to content

CVS to MySQL


PandaPHP

Recommended Posts

I have a CVS file with about 1000 entries which I need to input into a MySQL database. Each line in the file has 5 fields but I am only interested in the first two. These are the description and name.

 

I'm sure I can do this directly with MySQL but I need to use PHP anyway because there is actually other things I need to do with the data. I don't need to go into details because I already have that sorted.

 

I just need to know how to open the CVS file and make a mysql insert loop with the data.

 

Thanks

Link to comment
Share on other sites

may not be the neatest but it works for me

you can upload a csv file and split it up and insert it into a mysql file

this routine creates a master file and a subfile linked to the master file

just adapt it as needed

 

$inputfilename=$_FILES["file"]["tmp_name"];
	$handle = fopen("$inputfilename", "r");
	if ($handle) {
    			while (!feof($handle)) {
        					$buffer = fgets($handle, 4096);
        					if(!empty($buffer)){
        					$parts=explode(",",$buffer, 4);
        					$date=htmlspecialchars($parts[0]);
        					$parts2=explode("/",$date,2);
        					//$finished_date=$parts2[0]."-".$parts2[1]."-".$li_16;
        					$finished_date=$li_16."-".$parts2[0]."-".$parts2[1];
        					$parts3=explode("$",$parts[2],2);
        					$amount=htmlspecialchars($parts3[1]);
        					$desc=htmlspecialchars($parts[1]);
        					// remove any ' from the desc
        					$desc=preg_replace("/'/", "", $desc);
        					$user=$_SESSION['username'];
        					$query="INSERT INTO `blah_blah_blah` (
`id` ,
`bank_account` ,
`recepient` ,
`date_of_expense` ,
`date_expense_posted` ,
`apply_to_year` ,
`expense_mode` ,
`checknum` ,
`type_of_expense` ,
`reason` ,
`amount` ,
`signer` ,
`apply_to` ,
`receipt` ,
`refer` ,
`refer_to_user` ,
`cleared` ,
`complete` ,
`input_by` ,
`review_by` ,
`reviewed` ,
`cleared2` ,
`cleared_amount` ,
`complexity` ,
`omit` ,
`reconcile_link`
)
VALUES (
NULL , '$li_2', '$desc', '$finished_date', '$finished_date', '$li_16', '', '', '', '$desc', '$amount', '', '', '', '0', '', '0', '0', '$user', '', '0', '0', '', '0', '0', ''
)";
//echo $query."<br />";
mysql_query($query);
$lastid=mysql_insert_id();
$query="
INSERT INTO `blah_blah_blah_blah` (
`id` ,
`expense_id` ,
`date_of_split` ,
`date_split_cleared` ,
`reason` ,
`housing` ,
`split_amount` ,
`fund` ,
`account` ,
`project`,
`apply_to` ,
`tax_status`
)
VALUES (
NULL , '$lastid', '$finished_date', '$finished_date', '$desc', '0', '$amount', '', '', '','', ''
)";
mysql_query($query);
//echo $query."<br />";
        					}
    					}
    				fclose($handle);
}	


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.