1

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.

3
  • Remove the @ sign and see if you get an error Commented Jun 15, 2012 at 3:22
  • Hi John, I tried that as well. There wasn't any errors. Commented Jun 15, 2012 at 5:36
  • I removed the $stmt === false check for the error because sqlsrv_query() isn't returning false. So, there is an error, but it' just wasn't being caught by the if-statement of the sqlsrv_error() block. Commented Jun 15, 2012 at 16:52

2 Answers 2

3

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));
}
Sign up to request clarification or add additional context in comments.

5 Comments

Please look at my updated post above. sqlsrv_query() doesn't return false, but there is still an error originating from the MS's SQL for PHP 2.0 driver I'm using.
@user796837 Well the error message says "Invalid type", so I would go about checking whether all the inputs are correct. For instance, have you tried passing array('Scrollable' => SQLSRV_CURSOR_STATIC) instead of just static? I can't see how exactly you come to the conclusion, that this is a version problem. It just says, that IMSSP are errors originating from errors within the driver, not the database.
Hey mahok, I've narrowed down the problem to a word in my query (please note that this query works in MS SQL Server Management Studio). An Exmaple query looks like.. "SELECT * FROM [db_name].[dbo].[table_name] WHERE OrderID = '47725006';". It works for any table_name EXCEPT for table_name='Order'. For reason, [Order] is determined to be invalid.. any ideas?
Order is a reserver word, so I'm guessing this is causing you trouble. But since you escape it using the brackets, this should not be a problem...
Thank you for your responses. I've created a more succinct and relevant post here if you have any solution. stackoverflow.com/questions/11056616/…
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.