Solved

Updating one mysql field using a file

Posted on 2007-03-26
2
225 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now