sql 服务器插入数据 file_get_contents php

sql server insert data file_get_contents php

我有一个 php 代码可以插入 mysql 数据库数据和一个 blob (img and/or pdf)。

我必须将数据库迁移到 sql 服务器,当我尝试执行将数据插入 sql 服务器的代码时 table,sql 服务器显示我此错误:

 SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]No se permite la conversion implicita del tipo de datos nvarchar(max) a varbinary(max). Utilice la funcion CONVERT para ejecutar esta consulta.

代码如下:

$destino = addslashes( file_get_contents( $value['tmp_name'] ) );

                $tmpName = $value['tmp_name'];

                // Read the file

                $fp = fopen($tmpName, 'r');

                $data = fread($fp, filesize($tmpName));

                $destino = ($data);

                fclose($fp);

                $tamano = $value['size'];

                $tipo   = $value['type'];
                $name   = $value['name];

$sql="INSERT INTO documentacion 
        ( nombre, archivo,extension ) 
        VALUES 
        ( :nombre, :archivo, :extension)";


$valores = array(
    ':nombre'       =>$name,
    ':archivo'      =>utf8_encode( $destino ),
    ':extension'    =>$tipo,
);

try{

    $ejec=$bd->ejecuta($sql,$valores);  
} 
catch (PDOException $e) {

    echo 'Error BD al insertar documentacion: ' . $e->getMessage();
}
return $ejec[0];

任何人都可以告诉我如何将 nvarchar(max) 转换为 varbinary(max)。

谢谢

使用显式转换 https://msdn.microsoft.com/en-us/library/ms187928.aspx .

示例

declare @t table (val varbinary(max));

declare @s varchar(max) = 'Some text';

insert @t(val) values(cast(@s as varbinary(max)));

select cast(val as varchar(max))
from @t;

在你的代码中我认为应该是

VALUES 
   ( :nombre, cast(:archivo as varbinary(max)), :extension)