MS SQL通过PHP选择二进制字段

时间:2018-08-29 11:34:10

标签: php sql-server

在sql server 2016中,我有一些表。 其中之一是:

CREATE TABLE [dbo].[_Reference31](
[_IDRRef] [binary](16) NOT NULL,
[_Code] [nchar](9) NOT NULL,
[_Fld527RRef] [binary](16) NULL)

另一个是:

CREATE TABLE [dbo].[_Document8755](
    [_IDRRef] [binary](16) NOT NULL,
    [_Number] [nchar](9) NOT NULL,
    [_Fld8997RRef] [binary](16) NOT NULL,
    [_Fld8999] [datetime] NOT NULL,
    [_Fld9000] [datetime] NOT NULL)

PHP代码如下:

$data=array();
$STH = $DBHMS->prepare("SELECT _Fld534, _IDRRef AS id, _Code FROM _Reference31");
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($row=$STH->fetch()){
    $STH2 = $DBHMS->prepare("SELECT _IDRRef, _Number, _Fld8999, _FLd9000 FROM _Document8755 WHERE _Fld8997RRef=:id");
    $STH2->bindParam(':id',  $row['id'], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
    $STH2->execute();
    $STH2->setFetchMode(PDO::FETCH_ASSOC);
    $row['total_agreements']=$STH2->rowCount();
        while($row2=$STH2->fetch()){
            array_push($row['agreements'],$row2);
        }
        array_push($data,$row);
}
echo(json_encode($data));

问题在于total_agreements始终为零,但是当我在Management Studio中手动运行第二个查询时,它会起作用。

1 个答案:

答案 0 :(得分:0)

$STH2->bindParam(':id', hex2bin($row['id']), PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);

有效