mysqldatabasephpmyadminimport-csv

phpmyadmin import csv file. How to skip error line


I´m trying to import about 3gb csv files to phpmyadmin. Some of them contains more terminated chars and then importing stops because of wrong fields.

I have two colums which i want to fill. Im using : as terminanting char but when there is more of them in line it just stops. I cannot manage csv files they are too big. I want to skip error lines or look for other solutions. How can i do this ?

csv files looks like this

ahoj123:dublin
cat:::dog
pes::lolko

Solution

  • As a solution to your problem, I have written a simple PHP file that will "fix" your file for you ..

    It will open "test.csv" with contents of:

    ahoj123:dublin
    cat:::dog
    pes::lolko
    

    And convert it to the following and save to "fixed_test.csv"

    ahoj123:dublin
    cat:dog
    pes:lolko
    

    Bear in mind that I am basing this on your example, so I am letting $last keep it's EOL character since there is no reason to remove or edit it. PHP file:

    <?php
    
    $filename = "test.csv";
    $handle = fopen($filename, "r+") or die("Could not open $filename" . PHP_EOL);
    
    $keep = '';
    while(!feof($handle)) {
        $line = fgets($handle);
    
        $elements = explode(':', $line);
        $first = $elements[0];
        $key = (count($elements) - 1);
        $last = $elements[$key];
    
        $keep .= "$first:$last";
    
    }
    fclose($handle);
    
    
    $new_filename = "better_test.csv";
    $new_handle = fopen("fixed_test.csv", "w") or die("Could not open $new_filename" . PHP_EOL);
    fwrite($new_handle, $keep);
    fclose($new_handle);