0
我有一個插入到mysql數據庫數據和blob(img和/或pdf)的php代碼。sql server insert data file_get_contents php
我必須mirgate數據庫到SQL Server,當我嘗試執行代碼將數據插入到SQL Server表時,SQL Server表明我這個錯誤:
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)。
感謝
謝謝插入它很好,但我怎麼能顯示在網站頁面的斑點。在MySQL中,我有以下內容:'header(「Content-type:{$ doc-> extension}」); echo($ doc-> archivo);'($ doc-> archivo)是在sql select查詢中收到的值。我必須改變我的代碼與Sql服務器一起運行?再次感謝 – bcg
您可以用'select .. cast(archivo as varchar(max))archivo'將'varbinary'列轉換爲'varchar'並繼續進行與之前應用於初始文件內容的轉換(如果有)相反的轉換將它作爲參數傳遞給'insert'。 – Serg
插入的數組值爲'$ valores = array( ':nombre'=> $ name, ':archivo'=> utf8_encode($ destino), ':extension'=> $ tipo, ); '並繼續轉換以顯示img/pdf,我將是'utf8_decode($ doc-> archivo)'。我不確定我是否理解你,我做到了這一點,網頁返回給我一個沒有img/pdf的白頁。我不知道我在做什麼錯,但在MySQL工作正常。感謝您的迴應 – bcg