Load csv data in mysql database

Hello everyone, I am a novice in php and mysql so sorry in advance for the little knowledge I have in the field.
Like the subject says I have an assignment at school and have to upload selected columns from a csv file to a mysql database. I am trying the following:

[php]<?php

	// Create connection
	$conn = mysql_connect("localhost","username","password");

	// Check connection	
	if (mysqli_connect_errno($con))	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
	else
		echo "<p>Successful Connection!</p><br>";
		
	$sql_query = "CREATE TABLE tb1;
				LOAD DATA INFILE 'effy2010.csv' INTO TABLE tb1
				FIELDS TERMINATED BY ',' 
				ENCLOSED BY '\"'
				LINES TERMINATED BY '\r\n'
				IGNORE 1 LINES;";
	
	mysql_select_db('test');
	$retval = mysql_query( $sql_query, $conn );
	if(! $retval )
	{
		die('Could not load file: ' . mysql_error());
	}		
	echo "Table created successfully\n";		
	
	mysql_close($conn);

?>[/php]

I am getting a successful connection, but then I am really stuck with errors, when creating the file and loading the csv data, and don’t know what else to do here…
The error I get says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’’)’ at line 1

Any help would be greatly appreciated.
Thanks

This won’t solve you issue but you should do this

Change:
[php]CREATE TABLE tb1;
[/php]

To:
[php]CREATE TABLE IF NOT EXISTS tb1;[/php]

This will allow you to run your script more then 1 time…

Try this…

[php] $sql_query = “CREATE TABLE IF NOT EXISTS tb1;
LOAD DATA INFILE ‘effy2010.csv’ INTO TABLE tb1
FIELDS TERMINATED BY ‘,’
OPTIONALLY ENCLOSED BY '”’
LINES TERMINATED BY ‘\r\n’
IGNORE 1 LINES";
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service