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
Solved

Updating one mysql field using a file

Posted on 2007-03-26
2
238 Views
Last Modified: 2007-03-27
Hi everybody,

I'm trying to write a script that reads information from a txt file and updates a mysql table.

The table has 4 fields: test_id, buyid, amount and date.

And the txt looks like:
8965,13.80,20070315
12673,4.15,20070315
6363,10.85,20070315
.
.
In the first line 8965 is buyid, 13.80 is amount, and the rest is the date.

And this is what I’m trying to do: I need to compare buyid in the file and in the database, if they are the same then I have to update amount in the database using the amount info in the file.

I seemed not very complicated, but now I’m totally lost. Probably my approach is failing from the beginning. Can anybody please tell me what’s wrong with the following code, and what should I do to get it right??

Thanks a million in advance!


<?

# database connection
mysql_connect("localhost", "user", "pass");
mysql_select_db("database");

# read the file into an array

$lines = file_get_contents("file.txt");

// split the phrase by any number of commas or space characters

$pieces = preg_split("/[\s,]+/", $lines);


// read the table
$query = "SELECT test_id, buyid, amount, date FROM test";

$result = MYSQL_QUERY($query);
$howmany = MYSQL_NUMROWS($result);


for ($i=0; $i < $howmany; $i++) {

$buyid = mysql_result($result,$i,"buyid");
$amount = mysql_result($result,$i,"amount");


for ($n=1; $n < $howmany; $n+=3) {

$a= $pieces[$n];
if ($buyid == $a){
mysql_query("UPDATE ztest SET amount='$a'" );
}
}

}

?>
0
Comment
Question by:Dada44
  • 2
2 Comments
 
LVL 29

Expert Comment

by:TeRReF
ID: 18795794
file_get_contents() doesn't produce an array but a string. Use file() to read the file into an array...
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 18796001
I've looked at the code a bit more. I think this version will work better. You can skip some checks since UPDATE will only work if a record already exists anyway:

<?php

# database connection
mysql_connect("localhost", "user", "pass");
mysql_select_db("database");

# read the file into an array

$lines = file("file.txt");
$buyids = array();
$amounts = array();
foreach ($lines as $line) {
        echo $line.'<br>';
        $pos = strpos($line, ',');
        $buyids[] = substr($line, 0, $pos);
        $amounts[] = substr($line, $pos + 1, strpos($line, ',', $pos + 1) - $pos - 1);
}

$count = count($buyids);
for ($i = 0; $i < $count; $i++) {
        mysql_query("UPDATE ztest SET amount='".$amounts[$i]."' WHERE buyid=".$buyids[$i]);
}

?>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

792 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