如果我从表单中获取数据,我可以使用以下代码检查重复项,但是当我导入csv文件时,如何检查重复项。
这里的select(=)只会对一个值有效,但如果有25行我会怎么做?
$check = mysql_query("SELECT * FROM table WHERE x= '$Thd'");
if ($x)
{
if(mysql_num_rows($check) != 0)
{
header('location:nw.php?msg=cat exists');
}
else
{
)";
提前致谢。任何帮助表示赞赏。
答案 0 :(得分:1)
答案 1 :(得分:0)
mysql_query返回一个资源对象,可以通过迭代来获取每一行数据。请参阅the resource documentation中的示例,以获得良好的讨论。
答案 2 :(得分:0)
好吧,你正在从CSV文件中提取数据。我通常使用fopen / fgets而不是PHP的finicky fgetcsv(),但无论如何,检查重复值所需要做的与上面的相似。如果我知道唯一的字段是什么,那么举一个例子会更容易,但是:
$check = mysql_query("SELECT `x` FROM `table` WHERE `x` = '$uniquecsvfield' LIMIT 1");
//LIMIT 1 will stop it from searching for more, since you only need to know if one result exists.
//If you don't do LIMIT 1 then it will go through the whole table even if it finds one.
//Can also do 'WHERE x = $uniquecsvfield1 AND y = $uniquecsvfield2, whatever, depending on how
//many Fields need to be unique
if(mysql_num_rows($check) == 0) //If no rows are returned
{
mysql_query("INSERT INTO `table` VALUES blah blah blah"); //Insert
}
//Since you're gonna be processing the whole file, probably don't need to do anything if there is a
//duplicate, it just won't insert it.
//You could do an else that echo out "Duplicate found" or whatever