Web Hosting Talk







View Full Version : reading lines with php


JMele
09-24-2009, 06:37 PM
Hi guys,

I've been working on a PHP project and while i'm almost done i have no clue whatsoever how im going to accomplish this.

im looking to ready the contents of a file and put it into a database by each line.

like if i have a file with

xxx-xxx-xxx-xxx
xxx-xxx-xxx-xxx
xxx-xxx-xxx-xxx

i just wanna take each line and insert it into a database.

is it hard? i'm sure its possible... can anyone point me in the right direction??

thanks.

tim2718281
09-24-2009, 07:22 PM
Hi guys,

I've been working on a PHP project and while i'm almost done i have no clue whatsoever how im going to accomplish this.

im looking to ready the contents of a file and put it into a database by each line.

like if i have a file with

xxx-xxx-xxx-xxx
xxx-xxx-xxx-xxx
xxx-xxx-xxx-xxx

i just wanna take each line and insert it into a database.

is it hard? i'm sure its possible... can anyone point me in the right direction??

thanks.

Within PHP, you can use the SQL INSERT statament.

You can find an SQL tutorial at http://www.w3schools.com/sql/default.asp

JMele
09-24-2009, 07:31 PM
I can do SQL without a problem.

my problem is reading each line individually and then inserting JUST that line into the database.

tim2718281
09-24-2009, 08:25 PM
I can do SQL without a problem.

my problem is reading each line individually and then inserting JUST that line into the database.

Can you write PHP code that reads a file a line at a time into a PHP variable?

Can you write PHP code that executes an SQL INSERT statement?

Here's a link to a PHP SQL tutorial:

http://www.w3schools.com/php/php_mysql_insert.asp

JMele
09-24-2009, 08:28 PM
Can you write PHP code that reads a file a line at a time into a PHP variable?

Can you write PHP code that executes an SQL INSERT statement?

Here's a link to a PHP SQL tutorial:

http://www.w3schools.com/php/php_mysql_insert.asp

the insert is not my problem for the second time,

i need to read the document going through it pulling out each line individually.

squirrelhost
09-24-2009, 08:30 PM
foreach(file('filename') as $line)
{
$line = chop($line);
// now do something with $line
}

JMele
09-24-2009, 08:31 PM
foreach(file('filename') as $line)
{
$line = chop($line);
// now do something with $line
}
thank you.

tim2718281
09-24-2009, 09:29 PM
thank you.

How big is the file?

JMele
09-24-2009, 09:29 PM
very small, 30 lines max, 28 chars each line.

tim2718281
09-24-2009, 09:59 PM
very small, 30 lines max, 28 chars each line.

The file() method will work fine for that. (It reads the entire file into an array in memory)

For large files, you can read a line at a time using fgets()

There's a list of PHP file system functions here:

http://www.w3schools.com/php/php_ref_filesystem.asp

Neseema M M
09-25-2009, 02:38 AM
while(false !==($line = fgets($fp))) {
//$line have a line from the file
}