i want to make a program where the csv file directly insert into the mysql database table.

i found the code:

include 'mysql-connect.php';
$handle = fopen ('datafile1.csv', 'r');
		while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE)
			$query = "INSERT INTO Book_details VALUES ('". implode("','", $data)."')";
 			$query = @mysql_query($query);


it working fine .

but i have to face problem when i specify the columns in the mysql table like

while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE)
			$query = "INSERT INTO Book_details(idt_no,book_name,author_name,publisher_name,condition,condition_note,price,cur,quantity) VALUES ('". implode("','", $data)."')";
 			$query = @mysql_query($query);

no value is insert in the data base???

mention that database have 12 columns and csv have 9.
i have to insert all 9 to selected 9 columns in the database.
plz help me....

Recommended Answers

All 6 Replies

Clear @ before mysql_query and see if it throws any error. Also, you can have

$result = mysql_query($query) or die(mysql_error());

to print the error when the query fails.

P.S. @ suppresses the errors, so you shouldn't use it in testing scenarios. That will make your life hard to know where exactly the error is.

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 'condition,condition_note,price,cur,quantity) VALUES ('989003000000','sandipans a' at line 1

this error will occur i cant understand the problem

Condition is a mysql reserved keyword. In order to use it, you have to use ` [don't confuse it for single quote]. ie., `condition`. In my opinion, use some other word instead of condition to avoid the same problem in future.
Here is a list of all the reserved keywords in mysql.

thank u very much friend.

thanks for the help.its working now.

I am glad I could help :)

in my csv file there is 9 columns of 5 different tables. whet i want to insert perticuler columns inti perticuler table then it gives error.
any solution???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.