Solved

Updating one mysql field using a file

Posted on 2007-03-26
2
239 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
[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
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

730 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