I wanted to know if data is present then there must be mysql update query else insert data into mysql table.
What I have done is, I am fetching data from .csv file into mysql using fgetcsv() function.
Then reading each row from csv & putting those four values in four variables & then firing update query.
SO how can I check that if one of the data is not present still update query is working(0 rows successfully updated) so I want to identify that data & insert that into table else if data present my update query will work.
I want to either update or insert data into product_entity_price table.
My script-:
if(($handle = fopen("/data/sample/items_0420.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 8000, ",")) !== FALSE)
{
$num = count($data);
for ($c=0; $c < $num; $c++)
{
$temp = $data;
$string = implode(";", $temp);
}
$pieces = explode(";", $string);
$col1 = $pieces[0];
$col2 = $pieces[1];
$col3 = $pieces[2];
$col4 = $pieces[3];
$query_insert = "INSERT INTO import_prices (customer_id, sku, price, website) ".
"VALUES($col1,'$col2',$col3,'$col4') ".
"ON DUPLICATE KEY UPDATE customer_id=$col1, price=$col3,website='$col4' ";
$result = mysql_query($query_insert);
$query_update = "UPDATE product_entity_price pep
JOIN catalog_product cp ON cp.entity_id = pep.entity_id
AND cp.sku LIKE '$col2' AND website_id = $col4
JOIN customer_group cg ON cg.customer_group_id = pep.customer_group_id AND cg.customer_group_code = $col1
SET cpetp.value = ".$col3.";";
mysql_query($query_update);
}
}
Check mysql command replace :
http://dev.mysql.com/doc/refman/5.0/en/replace.html