Let’s say that you have a text file of your customer’s name and some of the information which you want to be published dynamically into your customer website. Opening notepad and copying and typing it in to your database might be most tedious job. If you have systematically entered your customers’ name similar to the format below, then it will be a lot easier to you to enter the data with a help of simple PHP code.
# Rihcard Potter,”New york, USA”,Brownies,5.30
# Harry Gere,”Minisotta, USA”,Cookies,3.80
# Angelina wilson,”Virgina, USA”,Chocolates,6.50
The above example contains customer’s name, their place, things they purchased and price differentiated by comma (,), quotation and line breaks. The above file is saved in customers.txt
You can load the above text file using PHP.
It can be done with load data local infile, which has flexibility to handle different field and line delimiters, and can even handle fields that might or might not be encased in quotes.
To do so, I created a new table named customer and loaded customer.txt in to the table customers.
/* This will create a table named customers with columns, name, Place, product, price. */
create table customers (
tid int primary key auto_increment not null);
/*This will load customer.txt in to the table created above */
load data local infile “.. /customer.txt ” into table customers
fields terminated by “,” optionally enclosed by ‘”‘
(customer, place, product, price);
tid will create a new column which contains unique table ID.
To interact PHP with MySQL and understand the basic of using MySQL refer to my previous article about, How to create a MySQL database using PHP script.