I am trying to query data from MS SQL 2008 server. I am querying data from a SQL View . Not a SQL table.
Does anyone know if SQLSRV is able to query a View from SQL Server?
Sample code below. Works when I use a Table but not when using a SQL View .
I am using PHP Version 7 along with MS SQL 2008 R2.
Connection
$serverName = "LOC-SVR\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName","UID" => "svc_test","PWD" => "test");
$connect = sqlsrv_connect( $serverName, $connectionInfo);
Query Code
$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'];
}
Result
There is no Error. Only 1 row is returned from the view. In Microsoft SQL Server Management Studio, few hundred rows are returned.
Any help is appreciated.
Found a solution. http://php.net/manual/en/function.sqlsrv-fetch-object.php
I am able to query my View using this.
CODE
$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 />";
}
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.