sqlsrv_query doesn't return false on faulty T-SQL query

329 views Asked by At

I'm using sqlsrv to connect to a MSSQL database. A feature in a secured (and only available to a few people) intranet application provides a box to write T-SQL code and execute them to the database. At this moment I'm working on a feature to test the given query.

First the query is validated for proper intent (e.g. if INSERT , UPDATE , DELETE , SHOW , CREATE , etc. is found the query will not execute). If that succeeds I'm trying to test the query. When I provide a faulty SQL query I still get a resource as result from sqlsrv_query instead of false (as described as expected behavior here: http://php.net/manual/en/function.sqlsrv-query.php). Note: the query truly is false (funny sentence..), in SQL Server Management Studio the query fails as expected.

Below my function testQuery(string $query):

public static function testQuery($query) {
    sqlsrv_configure('WarningsReturnAsErrors', 1);
    $statement = sqlsrv_query(self::$instance, $query);

    if($statement === false) {
        $errors = sqlsrv_errors(SQLSRV_ERR_ALL);

        if(!is_null($errors)) {
            return $errors;
        }

        return false;
    }

    return true;
}

Use of or abandoning the sqlsrv_configure() doesn't make a difference. The database I'm querying is a MSSQL 2005 database.

Suggestions?

1

There are 1 answers

0
Ben On BEST ANSWER

I'm not sure what it was, maybe delay in the connection. I haven't changed anything but it's working now.