简体   繁体   中英

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

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 (eg 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM