0

I haven't found anything useful about my problem on google or on here.

Currently, I'm storing a CSV file from an URL to my server using cronjob and file_get_contents(); once per hour.

Right now there are several steps executed whenever a user loads the page, but it's all PHP and Javascript from there so it takes about 9 second to load the page with the end result (html table) as described here.

I want to change that by importing the CSV file stored on my server to a SQL database but I have no idea how to realise this. Tried converting it to an array with PHP and then storing it to a db like here, but it didn't work for me. How would you approach this problem?

Thanks, Innerwolf.

file looks like:

username,score,numWins,numKills,numKillsWallshot,numDeaths,numShots,highestSpree,timePlayed

/\ssa,14104,26,2113,0,867,28083,15,43695

"∀ЈAIIX",10166,18,2641,0,1291,34201,14,59346

i tried this new cron

<?php

function download_remote_file($file_url, $save_to)
            {
                $content = file_get_contents($file_url);
                file_put_contents($save_to, $content);
            }
            download_remote_file(//link', realpath(".//path") . '/dump.csv');

            if(!function_exists('str_getcsv')) {     ------|
                //define str_getcsv                        |until now, cron
                }                                          |job executes 
            }                                              |part above.
            $url = '//path/dump.csv';                      |
            $csvData = file_get_contents($url);            |this part was in
            $lines = explode(PHP_EOL, $csvData);           |index.htm before
            $array = array();                              |and gets exe-
            foreach ($lines as $line) {                    |cuted when
            $line = str_replace("\\", "&#92;", $line);     |reload
            $line = str_replace("#", "&#35;", $line);      |
            $array[] = str_getcsv($line);                  |
            }                                        ------|

            $fields = implode(', ', array_shift($array));
            $values = array();
foreach ($array as $rowValues) {
    foreach ($rowValues as $key => $rowValue) {
         $rowValues[$key] = mysql_real_escape_string($rowValues[$key]);
    }

    $values[] = "(" . implode(', ', $rowValues) . ")";
}


//mySQL connection values


// Create connection
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$query = "INSERT INTO ranking ($fields) VALUES " . implode (', ', $values);

if ($conn->query($query) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $query . "<br>" . $conn->error;
}

$conn->close();
?>
Community
  • 1
  • 1
Innerwolf
  • 57
  • 1
  • 7

0 Answers0