onthespot Posted July 25, 2009 Share Posted July 25, 2009 I have a text file, which consists of many rows of data. Each row has a name and a number. 2 fields therefore. How can I insert this into my database ? Link to comment https://forums.phpfreaks.com/topic/167382-text-file/ Share on other sites More sharing options...
onedumbcoder Posted July 25, 2009 Share Posted July 25, 2009 $filename= "data.txt"; $filehandler = fopen($filename, 'r'); $filedata= fread($filehandler, filesize($filename)); fclose($filehandler); $rows = explode("\n", $filedata); $first = true; foreach ($rows as $row) { $rowdata = explode('/\s+/', $row); if(!$first) $insertvalues .= ", "; $insertvalues .= "('" . $rodata[0] . "', '" . $rowdata[1] . "'); $first = false; } mysql_query("INSERT into table(name, id) VALUES(" . $insertvalues . ")"); Link to comment https://forums.phpfreaks.com/topic/167382-text-file/#findComment-882601 Share on other sites More sharing options...
abazoskib Posted July 26, 2009 Share Posted July 26, 2009 or if you want to do it much quicker, use LOAD LOCAL DATA INFILE..check it out in the mysql online docs Link to comment https://forums.phpfreaks.com/topic/167382-text-file/#findComment-883498 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.