I am using an MSSQL database connected through ODBC.
When using PDO::bindValue()
on a query that has a nested SELECT statement, it fails to bind values within the nested SELECT (no problem on the primary SELECT).
This is a piece of sample code that fails:
$stmt = $cmdb->prepare("SELECT ci.CI FROM dbo.cmdb_ci AS ci " .
"INNER JOIN dbo.cmdb_model AS m ON m.ModelID = ci.Modelid " .
"INNER JOIN dbo.cmdb_class AS c ON c.ClassID = m.Classid " .
"WHERE (c.ClassID = :classid) " .
"AND (ci.CI IN (SELECT ci2.CI " .
"FROM dbo.cmdb_ci AS ci2 " .
"INNER JOIN dbo.cmdb_ci_status AS st2 ON st2.CI = ci2.CI " .
"WHERE st2.LocationID = :locationid))");
$stmt->bindValue("classid", 13);
$stmt->bindValue("locationid", 1011);
$stmt->execute();
if ($rows = $stmt->fetchAll())
$stmt->closeCursor();
foreach ($rows as $row)
echo $row["CI"];
The error I get is:
SQLSTATE[22018]: Invalid character value for cast specification: 206 [Microsoft][SQL Server Native Client 11.0][SQL Server]Operand type clash: text is incompatible with int (SQLExecute[206] at /builddir/build/BUILD/php-5.4.16/ext/pdo_odbc/odbc_stmt.c:254)
If I leave out the bindValue()
for ":locationid" and insert '1011' directly into the query, the call completes without errors and with the correct results.
Is this a bug in PDO, or do I have to call bindValue() differently?