Mysql查询并查找是否存在值

时间:2014-08-13 17:57:58

标签: php mysql

我得到这个代码来比较和找到2个CSV文件(每行1列,每行1个EAN代码)与array_diff在两个方向上的差异:

<?php 
mysql_connect('LOCALHOST', 'db', 'passwd') or die(mysql_error()); 
mysql_select_db("db") or die(mysql_error()); 

$update_table = "ps_product"; 

$file1 = file('1.csv');
$file2 = file('2.csv');


$diff  =  array_diff($file1, $file2);
$diff2  = array_diff($file2, $file1);


$diffjoined = array_merge($diff, $diff2);

?>

现在我必须知道在合并数组中,数据库中是否有相应的值,然后将数量设置为0:

    If "a value inside array ($diffjoined) was found in DB"
  foreach "ean code found in DB"

mysql_query ("UPDATE $update_table SET quantity='0' WHERE ean13 IN ($diffjoined) ");

1 个答案:

答案 0 :(得分:1)

我假设您的$diffjoined是您的代码中的ean13列表。

您的SQL查询已关闭

$eans = "'".implode("','", $diffjoined)."'";
$q = "UPDATE $update_table SET quantity='0' WHERE ean13 IN ($eans)";
mysql_query($q);