PDO MSSQL - INSERT INTO检查重复项 - 命名占位符

In my script, i am parsing a URL with JSON, and with json_decode i'm creating an array.

What i want is, using PDO for mssql, everytime i am parsing this script, to check if the values that are about to be inserted in the DB are already existing and if not, insert them in the DB. I don't have a specific column to check against, i just want to check if what i am inserting, like every columns, are identical to what already exists in the DB.

What i have so far looks like this:

$json = file_get_contents($jsonurl,0,null,null);
$jsonArray = json_decode($json, true);

// Connection code goes here of course... $DBH = new PDO ... etc.

$query = "IF NOT EXISTS (SELECT * FROM dbo.table
        WHERE
        FirstName = :fn
        AND LastName = :ln
        AND Address = :addr
        )
    BEGIN
        INSERT INTO dbo.table 
        (FirstName, LastName, Address)
        VALUES
        (:fn, :ln, :addr)
    END";


$STH = $DBH->prepare($query);

// assign variables to each place holder
$STH->bindParam(':fn', $firstname);
$STH->bindParam(':ln', $lastname);
$STH->bindParam(':addr', $address);    

// Loop through every contacts in the array and grab the following values
foreach($jsonArray['contacts'] as $key => $val){    
    $firstname  = $val["properties"]["firstname"]["value"];
    $lastname       = $val["properties"]["lastname"]["value"];
    $address        = $val["properties"]["address"]["value"];

    $STH->execute();
}

The $jsonArray might include some null values, not sure if that can cause any issue..

So in the query above, if i manually put a FirstName value, for example, which doesn't exist, then it works, but when i put a value that exists in the bindParam array ($firstname), it prevents from putting ANY rows in the DB and using placeholders doesn't work either.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[07002]: [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error' in C:\xampp\htdocs\test-PDO.php:129 Stack trace: #0 C:\xampp\htdocs\test-PDO.php(129): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\test-PDO.php on line 129

UPDATE: When using

$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

it returns:

Warning: @'@'˜Ñ: in C:\xampp\htdocs\test-PDO.php on line 82 Warning: PDOStatement::execute(): SQLSTATE[07002]: COUNT field incorrect: 0 [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error in C:\xampp\htdocs\test-PDO.php on line 82

Any ideas?

Thank you in advance

You have to bind AND name them twice ... you cannot (re-)use e.g. :fn twice.

$STH->bindParam(':fn', $firstname);
$STH->bindParam(':ln', $lastname);
$STH->bindParam(':addr', $address);    

$STH->bindParam(':fn2', $firstname);
$STH->bindParam(':ln2', $lastname);
$STH->bindParam(':addr2', $address);    


$query = "IF NOT EXISTS (SELECT * FROM dbo.table
    WHERE
    FirstName = :fn
    AND LastName = :ln
    AND Address = :addr
    )
BEGIN
    INSERT INTO dbo.table 
    (FirstName, LastName, Address)
    VALUES
    (:fn2, :ln2, :addr2)
END";