Reputation: 4090
I'm encountering a strange issue, where sqlsrv_query()
returns a valid statement resource, but sqlsrv_fetch_array()
subsequently returns false. I tested my query ($q
) in MS SQL Server Management Studio, so I know it returns a result with a certain number of rows. The code I have is...
$r = @sqlsrv_query($dbcMssql, $q, array(), array('Scrollable'=>'static')); // works
$rowCount = sqlsrv_num_rows($r); // this shows there are correct number of rows
$row = sqlsrv_fetch_array($r, SQLSRV_FETCH_ASSOC); // $row is FALSE
I'm using the same bit of a code on another MSSQL database, and it is working without any problem. The only difference with this MSSQL DB is that it uses BIGINT for its primary key and some of the column names are different (not that this matters).
What am I doing wrong?
UPDATE:
Whilte $stmt
is not false, I looked at the error anyway, and it gives me
[0] => IMSSP
[SQLSTATE] => IMSSP
[1] => -35
[code] => -35
[2] => Invalid type
[message] => Invalid type
The IMSSP leads me to believe that, according to documentation at http://php.net/manual/en/function.sqlsrv-errors.php
, it's my SQL for PHP 2.0 driver that is causing the problem. I see that SQL for PHP 3.0 has been released. This could be the problem I suppose.. because the DB i'm trying to query is a newer version of MSSQL (2008? vs 2005) than the one I've been using previously without any problems. However, this driver has been compiled with VC9 only (2.0 was available for both), and my PHP 5.3 is compiled with VC6.. so it will not load. Does this mean I need to recompile it using the source code or re-install PHP with a VC9 version?
I don't know how to recompile, so I guess re-installing VC9 version of PHP 5.3 is the way to go for me right now.
Upvotes: 1
Views: 6237
Reputation: 624
This error 'Invalid Type' happens when the query returns a column type which PDO can't handle. For example you will get this when you try to
SELECT default_value FROM sys.parameters
as that column is type sql_variant. If it makes sense, rather CAST that column to varchar or whatever, or eliminate it from the query.
Upvotes: 1
Reputation: 17166
There is probably something wrong with your query. You can retrieve the error message with sqlsrv_errors()
, e.g. like this:
$stmt = sqlsrv_query($tsql);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
Upvotes: 3