Solved

Read txt file with php and save it to mysql database

Posted on 2011-02-14
5
435 Views
Last Modified: 2012-05-11
Hi,


I have a file on ftp: file.txt
I need a script to read it and save each line to different mysql cell, but same row.
Example:

FILE:
Sam
Pass
Other content
here

DATABASE:
id
name
password
comments

What I want here is: open the file, read it line by line and save it like so:
first line (Sam) to 'name' column
second line (Pass) to 'password' column
other lines (Other content here) to 'comments' column

I know I need to use fopen, fclose etc. but I need full script.


Thanks for any help.
0
Comment
Question by:Zado
5 Comments
 
LVL 5

Assisted Solution

by:_-MYFOX-_
_-MYFOX-_ earned 125 total points
ID: 34889839
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 125 total points
ID: 34890207
With the file format like this, you would have to use the file function, which reads every single row to a table and put it in the query like this:

<?php
    $f = file("file.txt");
    $query = "insert into mytable (name, password, comments) values ('".$f[0]."','".$f[1]."','".$f[2]."')";
    echo"$query";
    // here you would have to exec a query
    // $result = mysql_query($query) or die("something went wrong");
?>

Open in new window

0
 
LVL 8

Assisted Solution

by:rationalboss
rationalboss earned 125 total points
ID: 34896480
What is the separator for each record? Example:
Sam
password_of_sam
other_contents

Mike
password_of_mike
other_contents

Test
password_of_test
other_contents

Open in new window


Is it just a blank line just like what I have above?

Here is a code:

<?php
$delimiter = "\n\n"; // delimeter between records
$file = file_get_contents('users.txt');
$file = str_replace("\r","",$file);

$file = explode($delimiter,$file);

foreach ($file as $record) {
	$record = explode("\n",$record);
	$username = $record[0];
	$password = $record[1]
	$other_content = $record[2];
	// make sure you addslashes the above content if it contains apostrophes
	// mysql_query("INSERT INTO users (userid, username, password, other_contents) VALUES (NULL,'$username','$password','$other_content')") or die('error in mysql. '.mysql_error());
}
echo "Done! I've completed what you asked me to do!";
?>

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 34897029
If you are using the fairly standard "csv" type of file, PHP has a built-in function to help you do this.  Read the man page here to see if your file is workable with the function and post back if you have any specific questions about it.
http://us3.php.net/manual/en/function.fgetcsv.php
0
 
LVL 8

Author Comment

by:Zado
ID: 34899424
Thanks for your help
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question