How to insert data from txt to mySQL using php? -
i have in words.txt :
ada adanya adalah adapun
i want insert of them mysql table , make sure no whitespace inserted. table should :
id | word 1 | ada 2 | adanya 3 | adalah 4 | adapun
i've been searching in forum, can't find case mine.
thank help.
create file-handle fopen()
, check if valid handle, run loop fgets()
argument loop - have each line individually stored variable inside loop, in case $line
. run query insert line inside while-loop.
if ($file = fopen("words.txt", "r")) { while (($line = fgets($file)) !== false) { // run query // perhaps $line = trim($line) ? // pseudo-query: insert tablename (word) values ('$line') // $line content of each line individually */ } fclose($file); // close file @ end } else { /* file not opened */ }
you can use trim()
on each $line
remove whitespaces (would $line = trim($line);
).
also note when inserting values database, should use parameterized queries ensure avoid security issues (sql injection) , issues quotes.
Comments
Post a Comment