Solved

Read txt file with php and save it to mysql database

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

632 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