user975475
user975475

Reputation:

CSV to mysql loading

I'm trying to upload this csv file to mysql on my hosting.

The csv is built by two columns:

alias, id

and than each row contains the data.

Here is an image

a screenshot of my csv file

But the mysql rejects me. why is that?

Upvotes: 1

Views: 179

Answers (3)

Starx
Starx

Reputation: 78971

Solution using PHP

$file = 'path/to.csv'; 

$lines = file($file);
$firstLine = $lines[0];
foreach ($lines as $line_num => $line) {
    if($line_num==0) { continue; } //escape the header column
    $arr = explode(",",$line);
    $column1= $arr[0];
    $column2= $arr[1];
       
    echo $column1.$column2."<br />";
        //put the mysql insert statement here
}

Upvotes: 0

David Custodio
David Custodio

Reputation: 131

Create a table "test_table " with two fields "alias" and "id", then execute this command:

LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
IGNORE 1 LINES
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(alias, id);

if your are using phpMyAdmin use the import option of phpMyAdmin. Choose CSV file format

Upvotes: 3

sam yi
sam yi

Reputation: 4934

You can also open up csv from excel and generate series of insert statements. Not the best solution but might be useful if you're looking for something quick and dirty.

Upvotes: 1

Related Questions