Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 859149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:38:24+00:00 2026-05-15T08:38:24+00:00

I have this large (and oddly formatted txt file) from the USDA’s website. It

  • 0

I have this large (and oddly formatted txt file) from the USDA’s website. It is the NUT_DATA.txt file.

But the problem is that it is almost 27mb! I was successful in importing the a few other smaller files, but my method was using file_get_contents which it makes sense why an error would be thrown if I try to snag 27+ mb of RAM.

So how can I import this massive file to my MySQL DB without running into a timeout and RAM issue? I’ve tried just getting one line at a time from the file, but this ran into timeout issue.

Using PHP 5.2.0.

Here is the old script (the fields in the DB are just numbers because I could not figure out what number represented what nutrient, I found this data very poorly document. Sorry about the ugliness of the code):

<?

    $file = "NUT_DATA.txt";

    $data = split("\n", file_get_contents($file)); // split each line

    $link = mysql_connect("localhost", "username", "password");
    mysql_select_db("database", $link);

    for($i = 0, $e = sizeof($data); $i < $e; $i++)
    {
        $sql = "INSERT INTO `USDA` (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) VALUES(";
        $row = split("\^", trim($data[$i])); // split each line by carrot
        for ($j = 0, $k = sizeof($row); $j < $k; $j++) {
            $val = trim($row[$j], '~');
            $val = (empty($val)) ? 0 : $val;
            $sql .= ((empty($val)) ? 0 : $val) . ','; // this gets rid of those tildas and replaces empty strings with 0s
        }
        $sql = rtrim($sql, ',') . ");";
        mysql_query($sql) or die(mysql_error()); // query the db
    }

    echo "Finished inserting data into database.\n";

    mysql_close($link);

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:38:25+00:00Added an answer on May 15, 2026 at 8:38 am

    If you have to use PHP, you can read the file line by line using fopen and fgets

    <?
    
    $file = "NUT_DATA.txt";
    $fh = @fopen( $file, "r" );    // open the file for reading
    $link = mysql_connect("localhost", "username", "password");
    mysql_select_db("database", $link);
    
    while( !feof( $fh ) )
    {
        $data = fgets( $fh, 4096 );     // read line from file
    
        $sql = "INSERT INTO `USDA` (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) VALUES(";
        $row = split("\^", trim($data)); // split each line by carrot
        for ($j = 0, $k = sizeof($row); $j < $k; $j++) {
            $val = trim($row[$j], '~');
            $val = (empty($val)) ? 0 : $val;
            $sql .= ((empty($val)) ? 0 : $val) . ','; // this gets rid of those tildas and replaces empty strings with 0s
        }
        $sql = rtrim($sql, ',') . ");";
        mysql_query($sql) or die(mysql_error()); // query the db
    }
    
    echo "Finished inserting data into database.\n";
    
    fclose( $fh );
    
    mysql_close($link);
    
    ?>
    

    Check out the fgets documentation for more info

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this large txt file 8mb+ that is delimited and I need to
I have a large xml file that looks like this: 20120124 07:30:15.301, saving to
So I have this pretty large XML file (40MB) that I'll have to repeatedly
I have this large XML file. There is a field that I want to
I have read this article from High Scalability about Stack Overflow and other large
I have a large number of csv files that look like this below: xxxxxxxx
I have a large nested vector that look like this: import Data.Vector let x
I have this problem with tables from my app. When the description of a
I have this problem: I have a collection of small files that are about
I have this stylesheet , but the right and left margins are too large

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.