Help With My Code Update Mysql
Posted: Thu Jul 02, 2009 10:00 pm
Need help opening this csv file and updating Mysql ...
anyone have anyidea why this code does not work
its pulling the data from the csv ok . but
the loop for the mysql is not got the correct
values.
if i echo $list[product_id] they are not correct ids.
what do i have wrong here .
as you can see for some reason when i echo
$list[product_code]
its the same for every data entry...
the number echoed is the same ...
why?
anyone have anyidea why this code does not work
its pulling the data from the csv ok . but
the loop for the mysql is not got the correct
values.
if i echo $list[product_id] they are not correct ids.
what do i have wrong here .
Code: Select all
$link = mysql_connect($mysqlHost, $user, $password) or die('Could not
connect: ' . mysql_error());
$handle = fopen("DataFeed.csv", "r");
mysql_select_db($database, $link);
// loop content of csv file, using comma as delemiter
while (($data = fgetcsv($handle)) !== FALSE) {
$id = (int) $data[0];
$orgprice = floatval($data[2]);
$weight = floatval($data[15]);
if($orgprice <= 10) {
$price = ($orgprice / 0.30);
}else if ($orgprice <= 50) {
$price = ($orgprice / 0.50);
}else if ($orgprice <= 100) {
$price = ($orgprice / 0.60);
}else if ($orgprice <= 300) {
$price = ($orgprice / 0.75);
}else{
$price = ($orgprice / 0.85);
}
$query = 'SELECT product_id FROM cscart_products';
if (!$result = mysql_query($query)) {
continue;
}
if ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
// added so i can see the value for testing
echo $line[product_id] . "<br>";
$query = "UPDATE cscart_product_prices SET price='$price' WHERE product_id=$line[product_id]";
mysql_query($query);
if (mysql_affected_rows() <= 0) {
// no rows where affected by update query
}
} else {
// entry don't exists continue or insert...
}
mysql_free_result($result);
}
fclose($handle);
mysql_close($link);
as you can see for some reason when i echo
$list[product_code]
its the same for every data entry...
the number echoed is the same ...
why?