簡體   English   中英

SQL SQL中的MSSQL SQLSRV PHP查詢

[英]MSSQL SQLSRV PHP Query from SQL View

我試圖從MS SQL 2008服務器查詢數據。 我正在查詢SQL視圖中的數據。 不是SQL表。

有誰知道SQLSRV是否能夠從SQL Server查詢視圖?

示例代碼如下。 我使用但不使用SQL視圖時工作

我正在使用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中,返回幾百行。

任何幫助表示贊賞。

找到了解決方案。 http://php.net/manual/en/function.sqlsrv-fetch-object.php

我可以使用此查詢我的View。

$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 />";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM