doumangzhen7204 2015-06-09 09:31
浏览 301
已采纳

sqlsrv_query在错误的T-SQL查询时不返回false

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条回答 默认 最新

  • dsh8009271 2015-06-09 12:04
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug