Solved

Updating one mysql field using a file

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

775 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