Solved

Read txt file with php and save it to mysql database

Posted on 2011-02-14
5
443 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

765 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