2012-05-04 132 views
2

我似乎無法得到直接的答案。資源ID nvarchar(MAX)

我在SQLSRV數據庫中獲取類型爲nvarchar(MAX)的字段的內容,但是當我回顯從查詢返回的字段結果而不是內容時,我總是收到「資源ID#1」 。我對SQLSRV和PHP相當陌生,但根據我的理解,這個值是指向其他地方的指針,我需要的實際內容是?

如果是這樣,我該如何獲得這些數據。

我曾嘗試:

$sql = "SELECT * FROM table"; 
    $stmt= sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_STATIC)); 

$getNext = sqlsrv_fetch($stmt); 
$result = sqlsrv_get_field($stmt, 1); // this is the nvarchar(max) field 

echo $result; // this shows Resource id #1; 

// I tried 
//$resource = sqlsrv_fetch($result); // this creates errors 

謝謝。

回答

1

NVARCHAR(MAX)將爲您處理存儲問題,在表中存儲少於大約4000個字符的任何東西,但在表中的指針存儲在單獨位置的任何東西都會更大。好消息是SQL Server將返回的數據呈現給您,就好像它存儲在表中一樣,所以NVARCHAR(MAX)的用法不應該是您的問題的原因,它只是像普通的表字段一樣工作。

剛剛發現這一點,可以幫助...

/*Get the second field of the row as a stream. 
Because the default return type for a nvarchar field is a 
string, the return type must be specified as a stream. */ 
$stream = sqlsrv_get_field($stmt, 1, 
          SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR)); 
while(!feof($stream)) 
{ 
    $str = fread($stream, 10000); 
    echo $str; 
} 

http://msdn.microsoft.com/en-us/library/cc296207(v=sql.105).aspx

+0

那麼,如何獲得內容,而不是 「資源ID#1」? –

+0

您確定nvarchar(max)字段是索引1嗎? sqlsrv_get_field是基於零的。 –

+0

這就是當我回應該字段的內容時顯示的內容。 –