开发者

Check for duplicates when importing CSV in PHP

If i get data from a form,i can use the below code to check duplicates but when i import a csv file how do i check duplicates.

Here in select (=) will do for only one value but in excel if there are 25 rows how do i do it?

$check = mysql_query("SELECT * FROM table WHERE x= '$Thd'");

if ($x)
{
    if(m开发者_Python百科ysql_num_rows($check) != 0)
    {
    header('location:nw.php?msg=cat exists');
    }
    else
    {

    )";

Thanks in advance. Any help is appreciated.


You can refer this

Fgetcv

get in to an array then check it


mysql_query returns a resource object, which can be iterated through to get each line of data. Please see the examples in the resource documentation for a good discussion.


Alright, so, you're pulling in data from a CSV file. I usually use fopen/fgets for that rather than PHP's finicky fgetcsv(), but whatever, what you'll have to do to check for duplicate values is similar to what you have above. It would be easier to give an example if I knew what the fields that are unique are, but:

$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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜