2016-06-15 32 views
0

配列検索を使用して重複データの挿入をスキップします。配列検索PHP mysql PDO

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '053fb04a34907637530dcb86b9f121f5fe499821' for key 'id'"

$check=$this->pdo->prepare("select id from user_places"); 
$check->execute(); 
$a = $check->fetchAll(PDO::FETCH_COLUMN, 0); 

for ($i = 0; $i < count($output->results); $i++) { 
    if (array_search($data[$i]['id'], $a) == "") { 
     $query->bindparam(1,$data[$i]['id']); 
     $query->bindparam(2,$data[$i]['name']); 
     $query->bindparam(3,$data[$i]['lat']); 
     $query->bindparam(4,$data[$i]['lng']); 
     $query->bindparam(5,$data[$i]['place_id']); 
     $query->bindparam(6,$data[$i]['types']); 
     $query->bindparam(7,$data[$i]['vicinity']); 
     $query->execute(); 
    } 
} 

答えて

0

From the manual for array_search$data[$i]['id']が配列に(インデックス0、すなわち最初の要素を発見された場合は、真と評価されますarray_search($data[$i]['id'], $a) == ""を、チェックしている

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

しかし、それはPDOエラーがスローされます)