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 6374197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:27:29+00:00 2026-05-25T01:27:29+00:00

Long story short, if I break up a PHP script into tiny chunks, I

  • 0

Long story short, if I break up a PHP script into tiny chunks, I can eventually get all of my code to run. However, I’ve got a script right now which reads a huge CSV file and inserts each row into a MySQL database. Rather than having to go through the onerous process of splitting up the file every time I want to update my site, I just want to get this script to work the way I know it should.

I’ve gotten it to insert around 10,000 rows before on a different web server, but there are at least 7 times that in the file, and it craps out before it’s done.

So, the story is, on one server it stops before it’s supposed to, and on another it doesn’t run at all… it just chugs its way to a 500 error after about 30 seconds.

The Apache error log gives me these lines when the script dies:

[Tue Aug 23 13:09:04 2011] [warn] [client 71.168.85.72] mod_fcgid: read data timeout in 40 seconds
[Tue Aug 23 13:09:04 2011] [error] [client 71.168.85.72] Premature end of script headers: newcsvupdater.php

I’ve got these two lines going at the top of the script:

set_time_limit(0);
ini_set('memory_limit','256M');

because previously I was having a fatal memory allocation error, because apparently splitting a large file up into arrays is memory-intensive.

Here’s the insertion code:

$file = "./bigdumbfile.csv";  // roughly 30mb

$handle = fopen($file, r);

$firstentry = 0;

while($csv = fgetcsv($handle))
{
    if($firstentry == 0)
    {
        $firstentry++;  // skips the top row of field names
    }
    else
    {         

      // unimportant conditional code omitted

        $checkforexisting = mysql_query("SELECT * FROM DB_TABLE WHERE ".
                "id_one = '".$csv[0]."' AND id_two = '".$csv[2]."'");

        $checknum = mysql_num_rows($checkforexisting);

        if($checknum == 0)
        {
            if(!mysql_query("INSERT INTO DB_TABLE ".
                       "(id_one, data_one, id_two, data_two, ".
                        /* so on for 22 total fields */") 
              VALUES ('".addslashes($csv[0])."', '".
                         addslashes($csv[1])."', '".
                         addslashes($csv[2])."', '".
                         addslashes($csv[3])."' "/* ditto, as above */))
            {                    
                exit("<br>" . mysql_error());
            }
            else
            {
                print_r($csv);
                echo " insert complete<br><br>";
            } 
        }            
    }
}        

echo "<br><b>DB_TABLE UPDATED"; 

I’ve had to split up large tasks because of this before, and I’m pretty tired of it. I’m sure I’m doing plenty wrong, as I’m totally self-taught and generally write what amounts to spaghetti, so don’t hold back.

  • 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-25T01:27:30+00:00Added an answer on May 25, 2026 at 1:27 am

    To increase the time limit for your script, you will need to edit the virtual host configuration for your site:

    http://www.moe.co.uk/2009/08/17/php-running-under-mod_fcgid-read-data-timeout-in-40-seconds-on-plesk/

    (mod_fcgid’s timeout is overriding PHP’s timeout)

    To make your script faster (so you might not need to perform the above step, which might not be possible on shared hosting), try this:

    Prepare all of the information to be inserted in advanced to do a bulk insert. The query should look something like this:

    INSERT IGNORE INTO (id_one, data_one, id_two, data_two) VALUES
    (1, 'apple', 3, 'banana'),
    (4, 'pear', 5, 'orange)
    

    The IGNORE part should have the same effect of checking in advance if the record already exists (if it does, it just won’t be inserted and it will continue on to the next).

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

Sidebar

Related Questions

Long story short Say I have the following code: // a class like this
Long story short, I need to get the total scroll size for a text
Long story short, I'm using a buggy WordPress template and we're too far into
Long story short, I have a substantial Python application that, among other things, does
To make a long story short I have to use processing in a class
Long story short, I'm making a custom Swing component that's basically a JTable with
Long story short: I used reflector on the System.Security.Util.Tokenizer class, and there's loads of
Long story short, I'm developing a theme template for a blog that enables you
Long story short: on Chrome and Safari only, values from a login form are
Long story short: >>> re.compile(r\w*).match(uFrançais) <_sre.SRE_Match object at 0x1004246b0> >>> re.compile(r^\w*$).match(uFrançais) >>> re.compile(r^\w*$).match(uFranais) <_sre.SRE_Match

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.