- Using PHP and SQLSRV Driver
I have to display binary images from an SQL Server database based on patient ID. The patient IDs and images are located in two different databases.
From the first db I query for image IDs based on the Patient ID entered, and add the results into an array(). Then I would like to use this array of IDs to get the images from the second database.
Problem: I get the following error when using the array in the WHERE caluse of my sql statement:
Notice: Array to string conversion in…
I am really lost on this.
The following is my code:
<?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);
}
}
You can just send a raw array in a query like that. You need to turn it in to the expected string like
1,2,3,....Now i assume these are not user input and are an INTEGER type column in the other DB so quoting shouldnt be necessary, but in other case you would want to make sure you escape each value before imploding.