PandaPHP Posted October 6, 2010 Share Posted October 6, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215257-cvs-to-mysql/ Share on other sites More sharing options...
litebearer Posted October 6, 2010 Share Posted October 6, 2010 might look into http://www.java2s.com/Code/Php/File-Directory/Readtextfileintoanarrayinonefunction.htm Quote Link to comment https://forums.phpfreaks.com/topic/215257-cvs-to-mysql/#findComment-1119571 Share on other sites More sharing options...
RichardRotterdam Posted October 6, 2010 Share Posted October 6, 2010 You probably mean CSV instead of CVS. If so have a look at fgetcsv Quote Link to comment https://forums.phpfreaks.com/topic/215257-cvs-to-mysql/#findComment-1119575 Share on other sites More sharing options...
gevensen Posted October 6, 2010 Share Posted October 6, 2010 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); } Quote Link to comment https://forums.phpfreaks.com/topic/215257-cvs-to-mysql/#findComment-1119589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.