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

  • Home
  • SEARCH
  • 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 916071
In Process

The Archive Base Latest Questions

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

I am trying to read 738627 records from a flat file into MySQl. The

  • 0

I am trying to read 738627 records from a flat file into MySQl. The script appears to run fine, but is giving me the above memory errors.

A sample of the file is:

#export_dategenre_idapplication_idis_primary
#primaryKey:genre_idapplication_id
#dbTypes:BIGINTINTEGERINTEGERBOOLEAN
#exportMode:FULL
127667880285760002817317350
127667880285760002818261461
127667880285760002825372301
127667880285760002827785570
127667880285760002827930241
127667880285760002827987861
127667880285760002828089791
127667880285760002828168361
127667880285760002828192041
127667880285760002829144541
127667880285760002829351511

I have tried increasing the allowed memory using

ini_set("memory_limit","80M");

and it still fails. Do I keep upping this until it runs?

The code in full is

  <?php
    ini_set("memory_limit","80M");
    $db = mysql_connect("localhost", "uname", "pword");
    // test connection 
    if (!$db) { 
        echo "Couldn't make a connection!"; 
        exit; 
    } 
    // select database 
    if (!mysql_select_db("dbname",$db))

    {
        echo "Couldn't select database!"; 
        exit; 
    }
    mysql_set_charset('utf8',$db);

    $delimiter = chr(1);
    $eoldelimiter = chr(2) . "\n";
    $fp = fopen('genre_application','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

 $loop = 0;
while (!feof($fp)) {
  $loop++;
    $line = stream_get_line($fp,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue;  //Skip lines that start with # 
    $field[$loop] = explode ($delimiter, $line);
    $fp++;
$export_date = $field[$loop][0];
$genre_id = $field[$loop][1];
$application_id = $field[$loop][2];

$query = "REPLACE into genre_apps
(export_date, genre_id, application_id)
VALUES ('$export_date','$genre_id','$application_id')";
print "SQL-Query: ".$query."<br>";
       if(mysql_query($query,$db))
         {
          echo " OK !\n";
         }
        else
             {
              echo "Error<br><br>";
              echo mysql_errno() . ":" . mysql_error() . "</font></center><br>\n";
             }

    }

    fclose($fp);
    ?> 
  • 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-15T17:59:25+00:00Added an answer on May 15, 2026 at 5:59 pm

    Your loop fills the variable $field for no reason (it writes to a different cell on every loop iteration), thereby using up more memory with every line.

    You can replace:

    $field[$loop] = explode ($delimiter, $line);
    $export_date = $field[$loop][0];
    $genre_id = $field[$loop][1];
    $application_id = $field[$loop][2];
    

    With:

    list($export_date, $genre_id, $application_id) = explode($delimiter, $line);
    

    For improved performance, you could take advantage of the ability to insert several lines using REPLACE INTO by grouping N lines into a single query.

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

Sidebar

Related Questions

I am trying to read in a simple flat file, it has 738,627 records
Im trying to read some data from a binary file into a buffer allocated
Im trying to read data from a text file and loading it into a
I am trying to read bytes of image from the XML file but I
I am trying to read from a file and encrypt the data using AES
Trying to read in some information from the google maps api into my application
am trying to read web page from java code and its fine for general
When trying to read tokens from a file in C++, I receive a seg
Trying to read into an object the following XML file (can be changed) into
Im trying to read a text file, x bytes at a time into a

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.