Solved

Read txt file with php and save it to mysql database

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

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now