2012-12-24 30 views
0
  • 使用PHP和SQLSRV驅動

我不得不從基於患者ID的SQL Server數據庫顯示二進制圖像。患者ID和圖像位於兩個不同的數據庫中。PHP int數組WHERE子句 - 陣列要字符串轉換

從第一個db我根據輸入的患者ID查詢圖像ID,並將結果添加到數組()中。然後,我想使用這個ID數組來從第二個數據庫獲取圖像。

問題:在我的SQL語句的WHERE caluse使用數組時,我得到了以下錯誤:

注意:數組字符串轉換在...

我真的失去了對這個。

以下是我的代碼:

<?php 
// ------------------------------------------------------------ 
// SCANNED IMAGES SEARCH CLASS 
// used to retreive binary images from sql server database 
// ------------------------------------------------------------ 

class ScannedImages extends DbConnect { 
    // ------------------------------------------------------------ 
    // PROPERTIES 
    // ------------------------------------------------------------ 
    public $imageOutput = NULL; 

    // ------------------------------------------------------------ 
    // GET SCANNED IMAGE IDS FROM RIS BASED ON PATIENT ID 
    // ------------------------------------------------------------ 
    public function getImagesByPatientId($sentPatientId) { 

     // ------------------------------------------------------------ 
     // 1. GET IMAGE IDS AND PUT THEM IN AN ARRAY 
     // ------------------------------------------------------------ 

     // connect to [[[FusionRIS]]] database 
     $conn1 = $this->sqlSrvConnect_2(); 

     // get image IDs based on patient ID 
     $sql1 = "SELECT DocMgtImageID FROM tbDocMgtImagesAffiliations WHERE PatientID = $sentPatientId"; 
     $stmt1 = sqlsrv_query($conn1, $sql1); 

     // exit if there is problem retrieving the data 
     if($stmt1 === false) { 
      die(var_dump(sqlsrv_errors(), true)); 
     } 

     // image id array 
     $imageIdArray = array(); 

     // loop through the results 
     while($row1 = sqlsrv_fetch_array($stmt1, SQLSRV_FETCH_ASSOC)) { 
      $imageIdArray[] = $row1['DocMgtImageID']; 
     } 

     // ------------------------------------------------------------ 
     // 2. GET IMAGES BASED ON ARRAY OF IMAGE IDS 
     // ------------------------------------------------------------ 

     // connect to [[[DocMgmt]]] database 
     $conn2 = $this->sqlSrvConnect_1(); 

     // get images based on ids in array 33482 
     $sql2 = "SELECT ImageData FROM tbDocMgtImages WHERE DocMgtImageID IN ($imageIdArray)"; 
     $stmt2 = sqlsrv_query($conn2, $sql2); 

     // exit if there is problem retrieving the data 
     if($stmt2 === false) { 
      die(var_dump(sqlsrv_errors(), true)); 
     } 

     // convert binary to image 
     function data_uri($file, $mime) { 
      $base64 = base64_encode($file); 
      return "data:$mime;base64,$base64"; 
     } 

     // counter for image display 
     $count = 0; 

     // loop through the results 
     while($row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) { 
      $count++; 
      $this->imageOutput .= '<a href="#"><img src="'. data_uri($row2['ImageData'], 'image/jpeg') .'" alt=""><span>'. $count .'</span></a>'; 
     } 

     // free the statement and connection resources 
     sqlsrv_free_stmt($stmt1); 
     sqlsrv_close($conn1); 

     sqlsrv_free_stmt($stmt2); 
     sqlsrv_close($conn2); 
    } 
} 

回答

0

你可以只在那樣的查詢發送原始陣列。您需要將其轉換爲預期的字符串,如1,2,3,...

$query = sprintf("SELECT ImageData FROM tbDocMgtImages WHERE DocMgtImageID IN (%s)", implode(',', $imageIdArray)); 

現在,我認爲這些都不是用戶輸入,並在其他數據庫整數類型列,因此報價不應該是必要的,但在其他情況下,你要確保你爆之前逃脫的每個值。

+0

謝謝!我不知道這是可能的。你已經救了我的一天! – user1108996

0

你逝去$ imageIdArray到SQL,你應該破滅首先

1

這是因爲您試圖將數組解釋爲字符串。您需要通過執行類似操作來明確地將此轉換轉換爲字符串:

$sql2 = "SELECT ImageData FROM tbDocMgtImages WHERE DocMgtImageID IN (" . implode(',', $imageIdArray) . ")"; 
+0

我得到:解析錯誤:語法錯誤,意外的';' ...在線51 – user1108996

+0

抱歉遺漏了'implode()'的最後一個圓括號。它已被編輯。 –

+0

非常感謝! – user1108996