PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Will you please show the sample lines from your file if you are suing csv format. If you are using xls file, I guess it should be done in different way using COM.
<?php
$cn = mysql_connect("localhost", "root", "mppadmin") or die("error connecting!");
mysql_select_db("np_dictionary_db", $cn) or die("unknown db");
$query = "load data local infile 'resultset.csv' into table `tbl_headwords`
fields terminated by ','
enclosed by '\"'
lines terminated by '\n'
(hw_id, hw_name)";
mysql_query($query) or die(mysql_error());
?>
It works fine but the strange issue is that the topmost field names are inserted too. Like "hw_name" is also inserted .
$query = "load data local infile 'resultset.csv' into table `tbl_headwords`
fields terminated by ','
enclosed by '\"'
lines terminated by '\n'
ignore 1 lines
(hw_id, hw_name)";
$query = "load data local infile 'resultset.csv' into table `tbl_headwords`
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
ignore 1 lines
(hw_id, hw_name)";
$link = mysql_connect("localhost", "user", "pass");
mysql_select_db("db");
$file = $_FILES['excel_file']['name'];
@copy($_FILES['excel_file']['tmp_name'], "files/$file");
$query = 'LOAD DATA INFILE /www/trisha/admin/"'.$file.'" INTO TABLE student_info FIELDS TERMINATED BY "," LINES TERMINATED BY "\\r\\n";';
mysql_query($query);
@unlink($file);
Cheers,
Dibyendra
My purpose is
1; upload csv file
2; run load command and insert that file data into DB
2; remove that uploaded file
Please advise what I am doing mistake.
TIA!
I have just finished making the code as per your requirement and it now works fine ! But I haven't removed the uploaded file and just kept in cvs file. If you want to do that just use unlink() function.