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 ? Quote 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 . ")"); Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/167382-text-file/#findComment-883498 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.