I have to port a LAMP-based project that uses PHP-MSSQL to connect to a SQL Server db. It has to be ported to a WAMP-based machine that uses PHP-SQLSRV to connect to a SQL Server db.
I can get/set data by executing stored procedures. It seems like most types of input variables are pretty simple to get working. But I cannot get input params of type bit to work.
I have tried using various values in PHP that are 'truthy' or 'falsey', but non of them get me past the error:
'truthy' things that didn't work:
1"1"true"True"
'falsey' things that didn't work:
0"0"false"False"
I have tried using various ways to add an item to the $params array, including omitting optional arguments as well as defining the PHP and SQL data types. Nothing works.
My error message from SQL Server (converted to a JSON object) is:
[{
"0":"42000",
"SQLSTATE":"42000",
"1":8114,
"code":8114,
"2":"[Microsoft][SQL Server Native Client 10.0][SQL Server]Error converting data type varchar to bit.",
"message":"[Microsoft][SQL Server Native Client 10.0][SQL Server]Error converting data type varchar to bit."
}]
To generate this error message, I passed a value of 0 or 1, neither of which is a varchar or even a string.
To maintain maximum compatibility I do not want to alter the Stored Procedure to Cast/Convert data on SQL Server. I want this to work from PHP keeping Stored Procedure unchanged.
PHP code:
<?php
$link = sqlsrv_connect($host,Array("Database"=>$database,"UID"=>$username,"PWD"=>$password));
$sql = " { call StoredProcedureName ( @input_param_of_bit_type=? ) } ";
$param1 = 1;
$params = Array(
Array(&$param1, SQLSRV_PARAM_IN)
);
$stmt = sqlsrv_prepare($link,$sql,$params);
if ($stmt===false) {
// handle error
print_r(sqlsrv_errors,true);
} else {
if (sqlsrv_execute($stmt)===false) {
// handle error. This is where the error happens
print_r(sqlsrv_errors,true);
} else {
// success! It never gets here, though.
}
}
?>
CAST(0 AS BIT)to force the bit datatype.