Mitul Lakhani
Mitul Lakhani

Reputation: 192

Array search PHP mysql PDO

Using array search for skip duplicate data from inserting. But it throws PDO error:

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();
    }
}

Upvotes: 0

Views: 587

Answers (1)

iainn
iainn

Reputation: 17417

From the manual for array_search:

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.

You're checking array_search($data[$i]['id'], $a) == "", which will evaluate true if $data[$i]['id'] is found at index 0 (i.e. the first element in the array)

Try using this instead:

if (array_search($data[$i]['id'], $a) === false) {

Upvotes: 0

Related Questions