资源ID nvarchar(MAX)

时间:2012-05-04 14:32:39

标签: php sql nvarchar

我似乎无法直截了当地回答这个问题。

我在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 个答案:

答案 0 :(得分: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