Solved

Updating one mysql field using a file

Posted on 2007-03-26
2
242 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

695 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