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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:59:22+00:00 2026-06-11T21:59:22+00:00

I am trying to import a csv file into a mysql table and I

  • 0

I am trying to import a csv file into a mysql table and I currently have a script that is running line by line because I need to hash an id combined with another id as well as format the date for mysql format.

The csv file has MORE columns than I am currently importing. Is it easier to just import all columns?

I was reading about LOAD DATA INFILE (http://dev.mysql.com/doc/refman/5.1/en/load-data.html), but I am wondering how I can use this and hash the ids and format the date without doing row by row execution. My current script is taking way too long and causing site performance issues while running.

Here is what I have:

$url = 'http://www.example.com/directory/file.csv';
if (($handle = fopen($url, "r")) !== FALSE) 
{
fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
    $EvID = $data[0];
    $Ev = $data[1];
    $PerID = $data[2];
    $Per = $data[3];
    $VName = $data[4];
    $VID = $data[5];
    $VSA = $data[6];
    $DateTime = $data[7];
    $PCatID = $data[8];
    $PCat = $data[9];
    $CCatID = $data[10];
    $CCat = $data[11];
    $GCatID = $data[12];
    $GCat = $data[13];
    $City = $data[14];
    $State = $data[15];
    $StateID = $data[16];
    $Country = $data[17];
    $CountryID = $data[18];
    $Zip = $data[19];
    $TYN = $data[20];
    $IMAGEURL = $data[21];
    $URLLink = $data[22];

        $data[7] = strtotime($data[7]);
        $data[7] = date("Y-m-d H:i:s",$data[7]);

    if((($PCatID == '2') && (($CountryID == '217') or ($CountryID == '38'))) || (($GCatID == '16') or ($GCatID == '19') or ($GCatID == '30') or ($GCatID == '32'))) 
    {
            if(!mysql_query("INSERT IGNORE INTO TNDB_CSV2 
                (id, EvID, Event, PerID, Per, VName,
                     VID, VSA, DateTime, PCatID, PCat,                
                CCatID, CCat, GCatID, GCat, City,
                     State, StateID, Country, CountryID, Zip,
                TYN, IMAGEURL) VALUES
                ('".md5($EventID.$PerformerID)."','".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."','".addslashes($data[3])."','".addslashes($data[4])."',
                    '".addslashes($data[5])."','".addslashes($data[6])."','".addslashes($data[7])."','".addslashes($data[8])."','".addslashes($data[9])."',
                '".addslashes($data[10])."','".addslashes($data[11])."','".addslashes($data[12])."','".addslashes($data[13])."','".addslashes($data[14])."',
                    '".addslashes($data[15])."','".addslashes($data[16])."','".addslashes($data[17])."','".addslashes($data[18])."','".addslashes($data[19])."',
                '".addslashes($data[20])."','".addslashes($data[21])."')"))
            {                    
                exit("<br>" . mysql_error());
            }
    }
}
fclose($handle);
}

Any help is always greatly appreciated. Thanks in advance.

  • 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-06-11T21:59:23+00:00Added an answer on June 11, 2026 at 9:59 pm

    try optimising your scripts first. First off, never run single queries when importing unless you have no other choice, the network overhead can be a killer.

    Try something like (obviously untested and coded in the SO textbox, check brackets match e.c.t.):

    $url = 'http://www.example.com/directory/file.csv';
    if (($handle = fopen($url, "r")) !== FALSE) 
    {
    fgetcsv($handle, 1000, ",");
    
    $imports = array();
    
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        $EvID = $data[0];
        $Ev = $data[1];
        $PerID = $data[2];
        $Per = $data[3];
        $VName = $data[4];
        $VID = $data[5];
        $VSA = $data[6];
        $DateTime = $data[7];
        $PCatID = $data[8];
        $PCat = $data[9];
        $CCatID = $data[10];
        $CCat = $data[11];
        $GCatID = $data[12];
        $GCat = $data[13];
        $City = $data[14];
        $State = $data[15];
        $StateID = $data[16];
        $Country = $data[17];
        $CountryID = $data[18];
        $Zip = $data[19];
        $TYN = $data[20];
        $IMAGEURL = $data[21];
        $URLLink = $data[22];
    
            $data[7] = strtotime($data[7]);
            $data[7] = date("Y-m-d H:i:s",$data[7]);
    
        if((($PCatID == '2') && (($CountryID == '217') or ($CountryID == '38'))) || (($GCatID == '16') or ($GCatID == '19') or ($GCatID == '30') or ($GCatID == '32'))) 
        {
    
        $imports[] = "('".md5($EventID.$PerformerID)."','".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."','".addslashes($data[3])."','".addslashes($data[4])."',
                        '".addslashes($data[5])."','".addslashes($data[6])."','".addslashes($data[7])."','".addslashes($data[8])."','".addslashes($data[9])."',
                    '".addslashes($data[10])."','".addslashes($data[11])."','".addslashes($data[12])."','".addslashes($data[13])."','".addslashes($data[14])."',
                        '".addslashes($data[15])."','".addslashes($data[16])."','".addslashes($data[17])."','".addslashes($data[18])."','".addslashes($data[19])."',
                    '".addslashes($data[20])."','".addslashes($data[21])."')";
    
    
    
        }
    }
    
    $importarrays = array_chunk($imports, 100);
    foreach($importarrays as $arr) {
    
     if(!mysql_query("INSERT IGNORE INTO TNDB_CSV2 
                    (id, EvID, Event, PerID, Per, VName,
                         VID, VSA, DateTime, PCatID, PCat,                
                    CCatID, CCat, GCatID, GCat, City,
                         State, StateID, Country, CountryID, Zip,
                    TYN, IMAGEURL) VALUES ".implode(',', $arr)){
    
         die("error: ".mysql_error());
    
     }
    
     }
    
    fclose($handle);
    }
    

    Play around with the number in array_chunk, too big and it may cause problems like the query being too long (yes there is a configurable limit in my.cnf), too small and its unneccassary overhead.

    You could also drop the use of assign the $data[x] to variables as its a waste given how small the script is, just use the $data[x] directly in your query e.c.t. (wont give a massive improvement, but depending on your import size it could save a little).

    Next thing would be to use low priority inserts/updates, check out this for more info on that to get you started: How to give priority to certain queries?

    after all of that, you could think of mysql config optimisation’s, but that’s one for google to explain really as the best settings are different for everyone and their unique situations

    Edit: Another thing i’ve done before is if you have a lot of keys set up that aren’t required for the import, you can drop those keys temporarily and add them back when the script is done. This can yield good time improvements too, but as your working on a live database there are pitfalls to work around if you go down that route.

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

Sidebar

Related Questions

I am trying to import a CSV file into a MySQL database table. The
I am trying to import a .csv file into a table. I have figured
I'm currently trying to import about 10000 rows (from a CSV file) into an
I am writing the following script up import a csv file into a mysql
I'm trying to import a CSV files into my MySQL table using the following
I'm trying to import a very large .csv file (~4gb) into mysql. I was
I'm trying to import a CSV file into an array that I can use
I am trying to import a large CSV file into a MySQL database. I
I am trying to import a large csv file into MySQL. It's about 1.4
I am trying to import a CSV file into a MySQL 5.1 database. The

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.