title: 500 Internal Server Error for CSV to MySQL using PHP
body: <p>I'm new to PHP and MySQL (and webprogramming). Thanks to stackoverflow, I've managed to solve most of my problems, the answers you guys provide are excellent!</p>
  
  <p>This time I'm getting a 500 Internal Server Error each time I run my script. This script gets  86 csv files and load them into MySQL one by one. I've managed to get all of them loaded when I split them into 15 files at a time, but this is not an optimal solution as it doesn't work when trying to load them 15 at a time automatically. </p>
  
  <p>According to other stackoverflow solutions, the "LOAD DATA INFILE" option would solve the problem, however I'm on shared hosting and this option is not available. </p>
  
  <p>Please can you have a look at my script and tell me what I can improve to try
          
  
  <pre><code>    //for troubleshooting 
      ini_set('error_reporting', E_ALL);
      ini_set("display_errors","1"); 
      ini_set('max_execution_time', 0);
      ini_set('memory_limit', '-1');
  
      //Create connection
      $db = new Database();
  
      $db-&gt;query( "Select `CustomerId`
              FROM DimCustomer
              WHERE CanManageClients&lt;&gt;'Y' and CurrentCustomer ='Y' 
              ORDER BY CustomerNumber") ; //and `CustomerId` = '1798561799'
  
      $rows = $db-&gt;resultset() ;
  
      $numrows = $db-&gt;rowCount();
  
      if (!($numrows == 0))
      {              
          for ($row = 0; $row &lt; $numrows; $row++)
          {
              $client_cust_id = $rows[$row]["CustomerId"];
              $filePath = dirname(__FILE__) . '/KP/KP'.$client_cust_id.'.csv';
              $linecount =0;
              $data = array();
              $csvfile = fopen($filePath, 'rb');
              $db-&gt;beginTransaction();
              $db-&gt;query("INSERT IGNORE INTO STAGINGAwKeywordsPerformance
                      (`AccountCurrencyCode`,
                      `AccountDescriptiveName`,
                      `AccountTimeZoneId`,
                      `AdGroupId`,
                      `AdGroupName`,
                      `AdGroupStatus`,
                      `AdNetworkType1`,
                      `AdNetworkType2`,
                      `ApprovalStatus`,
                      `AverageCpc`,
                      `AverageCpm`,
                      `AveragePosition`,
                      `CampaignId`,
                      `CampaignName`,
                      `CampaignStatus`,
                      `Clicks`,
                      `ClickType`,
                      `ConversionRate`,
                      `Conversions`,
                      `ConversionValue`,
                      `Cost`,
                      `CostPerConversion`,
                      `Ctr`,
                      `CustomerDescriptiveName`,
                      `Date`,
                      `DestinationUrl`,
                      `Device`,
                      `ExternalCustomerId`,
                      `FirstPageCpc`,
                      `KeywordId`,
                      `Impressions`,
                      `IsNegative`,
                      `KeywordMatchType`,
                      `KeywordText`,
                      `MaxCpc`,
                      `MaxCpm`,
                      `OriginalMaxCpc`,
                      `PercentCpa`,
                      `PrimaryCompanyName`,
                      `PrimaryUserLogin`,
                      `QualityScore`,
                      `KeywordStatus`,
                      `TopOfPageCpc`,
                      `ValuePerConversion`,
                      `ViewThroughConversions`)
                      VALUES
                      (:AccountCurrencyCode ,
                      :AccountDescriptiveName ,
                      :AccountTimeZoneId ,
                      :AdGroupId ,
                      :AdGroupName ,
                      :AdGroupStatus ,
                      :AdNetworkType1 ,
                      :AdNetworkType2 ,
                      :ApprovalStatus ,
                      :AverageCpc ,
                      :AverageCpm ,
                      :AveragePosition ,
                      :CampaignId ,
                      :CampaignName ,
                      :CampaignStatus ,
                      :Clicks ,
                      :ClickType ,
                      :ConversionRate ,
                      :Conversions ,
                      :ConversionValue ,
                      :Cost ,
                      :CostPerConversion ,
                      :Ctr ,
                      :CustomerDescriptiveName ,
                      :Date ,
                      :DestinationUrl ,
                      :Device ,   
                      :ExternalCustomerId ,
                      :FirstPageCpc ,
                      :KeywordId ,
                      :Impressions ,
                      :IsNegative ,
                      :KeywordMatchType ,
                      :KeywordText ,
                      :MaxCpc ,
                      :MaxCpm ,
                      :OriginalMaxCpc ,
                      :PercentCpa ,
                      :PrimaryCompanyName ,
                      :PrimaryUserLogin ,
                      :QualityScore ,
                      :KeywordStatus ,
                      :TopOfPageCpc ,
                      :ValuePerConversion ,
                      :ViewThroughConversions )");
  
          while(!feof($csvfile)) 
              {
                  $data[] = fgetcsv($csvfile);
                  if ($linecount &gt; 1 )
                  {
                      $db-&gt;bind(':AccountCurrencyCode',$data[$linecount][0], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AccountDescriptiveName',$data[$linecount][1], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AccountTimeZoneId',$data[$linecount][2], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AdGroupId',$data[$linecount][3], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AdGroupName',$data[$linecount][4], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AdGroupStatus',$data[$linecount][5], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AdNetworkType1',$data[$linecount][6], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AdNetworkType2',$data[$linecount][7], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':ApprovalStatus',$data[$linecount][8], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AverageCpc',$data[$linecount][9], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AverageCpm',$data[$linecount][10], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':AveragePosition',$data[$linecount][11], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':CampaignId',$data[$linecount][12], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':CampaignName',$data[$linecount][13], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':CampaignStatus',$data[$linecount][14], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':Clicks',$data[$linecount][15], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':ClickType',$data[$linecount][16], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':ConversionRate',$data[$linecount][17], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':Conversions',$data[$linecount][18], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':ConversionValue',$data[$linecount][19], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':Cost',$data[$linecount][20], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':CostPerConversion',$data[$linecount][21], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':Ctr',$data[$linecount][22], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':CustomerDescriptiveName',$data[$linecount][23], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':Date',$data[$linecount][24], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':DestinationUrl',$data[$linecount][25], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':Device',$data[$linecount][26], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':ExternalCustomerId',$data[$linecount][27], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':FirstPageCpc',$data[$linecount][28], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':KeywordId',$data[$linecount][29], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':Impressions',$data[$linecount][30], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':IsNegative',$data[$linecount][31], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':KeywordMatchType',$data[$linecount][32], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':KeywordText',$data[$linecount][33], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':MaxCpc',$data[$linecount][34], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':MaxCpm',$data[$linecount][35], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':OriginalMaxCpc',$data[$linecount][36], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':PercentCpa',$data[$linecount][37], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':PrimaryCompanyName',$data[$linecount][38], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':PrimaryUserLogin',$data[$linecount][39], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':QualityScore',$data[$linecount][40], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':KeywordStatus',$data[$linecount][41], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':TopOfPageCpc',$data[$linecount][42], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':ValuePerConversion',$data[$linecount][43], PDO::PARAM_STR, 20);
                      $db-&gt;bind(':ViewThroughConversions',$data[$linecount][44], PDO::PARAM_STR, 20);
  
                      $db-&gt;execute();
                  }
                  $linecount++;                   
              }
              $db-&gt;endTransaction();
              fclose($csvfile);
          }
      }
  
      $db = NULL;
  
      die("END");         
      ?&gt;
  </code></pre>