Page 1 of 1

Can PHP script read a text file

Posted: Tue Jan 29, 2008 4:36 pm
by crimmelcp
Can PHP script read a text file

Can someone show me?

I need to import a text file into a sql database

Thanks
Charlie Crimmel

Re: Can PHP script read a text file

Posted: Tue Jan 29, 2008 4:47 pm
by s.dot
file_get_contents()
fopen(), fread(), fclose()
fgetcsv()

and for mysql LOAD DATA IN FILE

Re: Can PHP script read a text file

Posted: Tue Jan 29, 2008 6:33 pm
by crimmelcp
Thanks:
This is my first attempt to use MySQL and PHP
If I had a Vendor text file with the following fields
Vendor_ID , Vendor_Number , Vendor_name, Address1, Address2, City, State, Zip
the data might look like
0001,"V12345","Joe Smith","123 Any place Street","Apt. 123","Any City","WV","11111"
0002,"V23456","John Smith","234 Any place Street","Apt. 123","Any City","OH","2222"
0003,"V34567","Bob Smith","345 Any place Street","Apt. 123","Any City","KY","33333"
0004,"V45678","Mary Smith","456 Any place Street","Apt. 123","Any City","IN","44444"
0005,"V56778","Sue Smith","567 Any place Street","Apt. 123","Any City","TN","55555"

What would be the syntax to read this text file and import it into the Vendor SQL Table with the same fields.

Thanks
Charlie Crimmel

Re: Can PHP script read a text file

Posted: Tue Jan 29, 2008 6:42 pm
by dayyanb
crimmelcp wrote:Thanks:
This is my first attempt to use MySQL and PHP
If I had a Vendor text file with the following fields
Vendor_ID , Vendor_Number , Vendor_name, Address1, Address2, City, State, Zip
the data might look like
0001,"V12345","Joe Smith","123 Any place Street","Apt. 123","Any City","WV","11111"
0002,"V23456","John Smith","234 Any place Street","Apt. 123","Any City","OH","2222"
0003,"V34567","Bob Smith","345 Any place Street","Apt. 123","Any City","KY","33333"
0004,"V45678","Mary Smith","456 Any place Street","Apt. 123","Any City","IN","44444"
0005,"V56778","Sue Smith","567 Any place Street","Apt. 123","Any City","TN","55555"

What would be the syntax to read this text file and import it into the Vendor SQL Table with the same fields.

Thanks
Charlie Crimmel
Use preg_match_all to extract the individual values, then insert them into your database.

Re: Can PHP script read a text file

Posted: Tue Jan 29, 2008 9:00 pm
by crimmelcp
Thanks:
If I were doing this in another programming language I would DIM variables
DIM vVendor_ID as N
Dim vVendor_Number as C
Dim vVendor_name as C
Dim vAddress1 as C
Dim vAddress2 as C
Dim vCity as C
Dim vState as C
Dim vZip as C
Then initializee the variables
vVendor_ID = 0
vVendor_Number =""
vVendor_Name=""
vAddress1=""
vAddress2=""
vVendor_City=""
vVendor_State=""
vVendor_Zip=""
Then open the file
read the first record
populate the variables
write the record to the Table
read the next record
loop until the EOF

Is this the same concept as preg_match_all
or does PHP have a way to do it like described above.

Thanks
Charlie Crimmel

Re: Can PHP script read a text file

Posted: Tue Jan 29, 2008 9:17 pm
by s.dot
You would be much better off using fgetcsv() as it can detect individual values using your comma delimeters.

There's some good examples on the linked page.

Re: Can PHP script read a text file

Posted: Tue Jan 29, 2008 9:30 pm
by dayyanb
scottayy wrote:You would be much better off using fgetcsv() as it can detect individual values using your comma delimeters.

There's some good examples on the linked page.
But I wrote a regex : (. /(?:"?(.*?)"?)(?:,|\n|\r|$)+/