Two parameters right next to each other in a PDO query?

mysql, pdo, php

Solution

What you wrote there is an integer subtraction. So you are writing the result of `par1` minus `par2` into `col`.

Instead you should create the string `$par1 . '-' . $par2` outside of PDO and then just pass it in via some other named parameter:

$stmt = $pdo->prepare('... SET col = :col');
$stmt->execute(['col' => $par1 . '-' . $par2]);

Problem

I have run across a problem. When I use PDO::prepare() to build a certain query, then pass parameters to execute, the query will execute correctly but one of the parameters does not seem to be inserted into the database. The prepare statement looks like this: ``` ... SET col = :par1-:par2 ... ``` So what I'm trying to do is put the value "[par1]-[par2]" into the column in the database. The problem is that the first parameter is not being stored in the database, but the dash and the second parameter are. So the resulting value being stored in the database from the above query is "-[par2]". Why would that be?

Original source