Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php to mysql

Posted on 2009-02-21
15
Medium Priority
?
516 Views
Last Modified: 2012-05-06
in a .txt document

firstname

lastname

email address


firstname

lastname

email address


how could someone read this data in php and put it on a mysql table


0
Comment
Question by:jerseyguy0
[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
  • 4
  • 3
  • 2
  • +4
15 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 23700410
just to clarify: the data is 1 line at a time, or are the 3 information in 1 line each time? in the latter case, what is the separator?

now: EE is not a rent-a-coder, so you should try to clarify/check out the different parts of such a "simple" problem yourself:
* read a file
* split a string into parts (not necessarily required here...)
* write a sql statement for the INSERT
* run the sql statement

so, read up the sites php.net and mysql.com to get basic samples, and come back with concrete problems you have then, posting the code you have then, and the errors/problems you run into.
0
 
LVL 1

Author Comment

by:jerseyguy0
ID: 23700448
one line as a separator between firstname, lastname and email address

two lines as a separator between block of firstname lastname and email address
0
 
LVL 8

Expert Comment

by:agamal
ID: 23700455
please post a sample text file ... to send you the exact processing code :D
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:OrTzabary
OrTzabary earned 200 total points
ID: 23700466
Read the file with fopen & fread (read in php.net)

after that explode the string with \n (new line seperator)

do foreach to work over the array and insert into the database with mysql query

example included...
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
 
$content_array = explode("\n", $contents);
 
foreach ($content_array AS $name => $value) {
mysql_query("INSERT INTO table SET field = '$value'");
}

Open in new window

0
 
LVL 1

Author Comment

by:jerseyguy0
ID: 23700482
attached is txt file of firstname lastname emailadress
firstnamelastnameemail.txt
0
 
LVL 19

Assisted Solution

by:Michael701
Michael701 earned 400 total points
ID: 23700497
Something like this should get you started. I'll have to verify the field names of your database table.


$link = mysql_connect("localhost", "mysql_user", "mysql_password") 
       or die("Could not connect: " . mysql_error()); 
 
$handle = fopen ("/tmp/inputfile.txt", "r"); 
while (!feof ($handle)) { 
   $first_name = mysql_real_escape_string(fgets($handle)); 
   $last_name =  mysql_real_escape_string(fgets($handle)); 
   $email =      mysql_real_escape_string(fgets($handle)); 
   
   $sql_command="insert into customers (first_name, last_name, email ) values ('$first_name', '$last_name', '$email')";
   $ok=mysql_query($sql_command);
} 
fclose ($handle); 

Open in new window

0
 
LVL 8

Assisted Solution

by:agamal
agamal earned 400 total points
ID: 23700589
try this ... change the variable of course

<?php
//reading file
$filename = "firstnamelastnameemail.txt";
$file = file($filename);
 
//database settings
$db = "test";
$table = "users";
$col1 = "first";
$col2 = "last";
$col3 = "mail";
 
//connect to mysql
$connection = mysql_connect('localhost', 'root', 'root') or die (mysql_error());
$db = mysql_select_db('test', $connection) or die (mysql_error());
 
//do the reading loop
$j = 0;
$result = array();
for ($i=0;$i< count($file);$i++) {
if ($j == 3)
{
//do the sql query
$query  = "insert into $table (`$col1` , `$col2` , `$col3` ) values ('$result[0]', '$result[1]', '$result[2]')";
$res = mysql_query($query) or die('Error, query `' . $query . '` failed');
$j = 0;
}
if ($file[$i] != "\n") {
$result[$j] = $file[$i];
$j++;
}
        }
 
        mysql_close($connection);
 
 
?>

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 23700674
I downloaded the test data.  Are there really blank lines in the input file?  Thanks, ~Ray
0
 
LVL 1

Author Comment

by:jerseyguy0
ID: 23700744
to ray_passeur

yes there are blank lines in the input file
1 blank line between firstname lastname emailaddress

2 blank lines between new people
0
 
LVL 1

Author Comment

by:jerseyguy0
ID: 23700746
to agamal:
try this ... change the variable of course


which variable should I change
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 400 total points
ID: 23700809
I would do something like this:
mysql_connect('localhost','user','pass') or die('Could not connect: '.mysql_error());
mysql_select_db('dbname') or die("Could not select database '$dbname'");
$data = file_get_contents('firstnamelastnameemail.txt');
$records = explode("\n\n\n",$data);
$inserted = 0;
foreach($records as $record) {
  list($fname,$lname,$email) = explode("\n\n",$record); 
  $fname = mysql_real_escape_string($fname);
  $lname = mysql_real_escape_string($lname);
  $email = mysql_real_escape_string($email);
  $res = mysql_query("insert into MyTable set fname='$fname',lname='$lname',email='$email'");
  if(!$res) echo 'Failed: '.$fname.' '.$lname.' '$email.'<br />'.mysql_error().'<br />';
  else $inserted++;
}
echo $inserted.' rows inserted.';

Open in new window

0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 400 total points
ID: 23700820
Sorry, error in line 12, a missing dot, it should be:

if(!$res) echo 'Failed: '.$fname.' '.$lname.' '.$email.'<br />'.mysql_error().'<br />';
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 23700829
How many records do you have in the live data input file?
0
 
LVL 19

Assisted Solution

by:Michael701
Michael701 earned 400 total points
ID: 23700894
you could just add a few read lines to my code

$first_name = mysql_real_escape_string(fgets($handle)); 
$blank=fgets($handle);
$last_name =  mysql_real_escape_string(fgets($handle)); 
$blank=fgets($handle);
$email =      mysql_real_escape_string(fgets($handle)); 
$blank=fgets($handle);
$blank=fgets($handle);

Open in new window

0
 
LVL 8

Assisted Solution

by:agamal
agamal earned 400 total points
ID: 23702266
you file name in
$filename = "firstnamelastnameemail.txt";
 
//database settings
Data base name
$db = "test";

table name
$table = "users";

first name column in your table
$col1 = "first";

last name column in your table
$col2 = "last";

email column in your table
$col3 = "mail";
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

609 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