0

I know this is an ugly idea, but how can I bypass this maximum execution error. I don't want to configure any .ini that I have read in some solutions.

I have this code to upload csv to sql database but this keeps me giving an error when uploading large files. Please help me revise my code guys

<?php
if(isset($_POST['submit'])) {
    $host = 'localhost';
    $user = 'root';
    $password = '';
    $db = 'jeremy_db';

    $con = mysqli_connect($host,$user,$password) or die('Could not' .mysqli_error($con));

    mysqli_select_db($con, $db) or die ('Could not' .mysqli_error($con));

    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file, "r");
    $c = 0;

    while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
        $sha1 = $csvdata[0];
        $vsdt = $csvdata[1];
        $trendx  = $csvdata[2];

        $sql = "INSERT INTO jeremy_table_trend (sha1,vsdt,trendx) VALUES ('$sha1','$vsdt','$trendx')";
        $query = mysqli_query($con , $sql);

        $c = $c+1;
    }
    if($query){
        echo "success"; 
    }
    else { 
        echo "error";
    }
}
?>

2 Answers2

0

try this

add this line to your code

ini_set('max_execution_time', 300); //300 seconds = 5 minutes //whatever you want

Taken from How to increase maximum execution time in php

Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
  • OP is against altering the config settings. ;) – Rotimi Oct 31 '18 at 11:24
  • if its helpful to you than accept the answer so other use it – Bhargav Chudasama Oct 31 '18 at 11:25
  • @Akintunde-Rotimi What `ini_set('varName', 'varValue')` does is essentially override the INI config for the duration of the PHP script's execution. See the PHP documentation for more info. http://php.net/manual/en/function.ini-set.php – Will Oct 31 '18 at 11:27
  • @Will you need to read the question properly. OP is definitely confused. OP phrased the question as looking for ways to increase execution time without altering settings. This answer is clearly altering the settings just in a different way – Rotimi Oct 31 '18 at 11:29
  • But it doesn't notify me the success when the uploading is done –  Oct 31 '18 at 11:30
  • @Akintunde-Rotimi ` increase execution time without altering settings.` in `.ini file` not in `php script` – Bhargav Chudasama Oct 31 '18 at 11:30
0

Executing insert statements in the loop is expensive. You can try dumping the csv file directly into MySQL, that will take relatively lesser execution time.

Refer to this question

us190190
  • 116
  • 8