Solved

Read txt file with php and save it to mysql database

Posted on 2011-02-14
5
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

752 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