【问题标题】:MSSQL SQLSRV PHP Query from SQL ViewSQL 视图中的 MSSQL SQLSRV PHP 查询
【发布时间】:2017-09-06 01:48:39
【问题描述】:

我正在尝试从 MS SQL 2008 服务器查询数据。我正在从 SQL 视图 查询数据。不是 SQL 表。

有谁知道 SQLSRV 是否能够从 SQL Server 查询视图?

下面的示例代码。在我使用 Table 时有效,但在使用 SQL View 时无效。

我正在使用 PHP 版本 7 和 MS SQL 2008 R2。

连接

$serverName = "LOC-SVR\SQLEXPRESS"; //serverName\instanceName

$connectionInfo = array( "Database"=>"dbName","UID" => "svc_test","PWD" => "test");
$connect = sqlsrv_connect( $serverName, $connectionInfo);

查询代码

$sql = "SELECT Col1
             , Col2
             , Col3 
        FROM V_TEST_VIEW;"; 

sqlsrv_configure('WarningsReturnAsErrors', 0);

sqlsrv_query($connect,$sql);

$res = sqlsrv_query($connect, $sql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));  

while ($row = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC)) 
        {
          echo $row['Col1'];
          echo $row['Col2'];
          echo $row['Col3'];
        }

结果

没有错误。视图只返回 1 行。在 Microsoft SQL Server Management Studio 中,会返回几百行。

感谢任何帮助。

【问题讨论】:

  • 我使用 View whith MS SQL PostgreSQL 和 MYSQL 并且 createad 之后的视图被视为表格。唯一的区别在于您如何创建、更改和销毁它们。
  • 您创建的视图的代码在哪里?

标签: php sql-server sql-server-2008 php-7 sqlsrv


【解决方案1】:

找到了解决办法。 http://php.net/manual/en/function.sqlsrv-fetch-object.php

我可以使用它来查询我的视图。

代码

$sql = "SELECT Col1, Col2, Col3 FROM V_TEST_VIEW";
$stmt = sqlsrv_query( $connect, $sql);
if( $stmt === false ) {
 die( print_r( sqlsrv_errors(), true));
}

// Retrieve each row as an object.
// Because no class is specified, each row will be retrieved as a stdClass object.
// Property names correspond to field names.
while( $obj = sqlsrv_fetch_object( $stmt)) {
  echo "<strong>".$obj->Col1."</strong>, ".$obj->Col2 ."<br />";
}

【讨论】:

  • 问题可能与光标类型SQLSRV_CURSOR_KEYSET 有关。
猜你喜欢
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
相关资源
最近更新 更多