php - inserting csv file with different amount of columns into mysql table -
trying insert data csv file using php command fcsvget mysql table coming short can tell me im going wrong. im getting no data going in , errors when loading page.
the csv file has different header names , amount of columns table going into.
here code far.
<?php // set local variables $connect = mysql_connect("localhost","db","password") or die('could not connect: ' . mysql_error()); $handle = fopen("test.csv", "r"); // connect mysql , select database or exit mysql_select_db("db", $connect); // loop content of csv file, using comma delimiter while (($data = fgetcsv($handle, 1000, ",")) !== false) { $product_id = $data[0]; $model = $data[1]; // entry insert $query = "insert products_test (products_id, products_model) values '$product_id', $model"; mysql_query($query); if (mysql_affected_rows() <= 0) { // no rows affected update query } } else { // entry doesn't exist continue or insert... } mysql_free_result($result); } fclose($handle); mysql_close($connect); ?>
check code..
} else { // entry doesn't exist continue or insert... }
has no effect there no if started that. 1
if (mysql_affected_rows() <= 0) { // no rows affected update query }
is closed before 'else ' statement. not in query got in first look
Comments
Post a Comment