0
  • 使用 PHP 和 SQLSRV 驱动程序

我必须根据患者 ID 显示来自 SQL Server 数据库的二进制图像。患者 ID 和图像位于两个不同的数据库中。

从第一个数据库中,我根据输入的患者 ID 查询图像 ID,并将结果添加到 array() 中。然后我想使用这个 ID 数组从第二个数据库中获取图像。

问题:在我的 sql 语句的 WHERE 原因中使用数组时出现以下错误:

注意:数组到字符串的转换...

我真的很迷茫。

以下是我的代码:

<?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);
    }
}
4

3 回答 3

1

那是因为您试图将数组解释为字符串。您需要通过执行以下操作明确地将此转换为字符串:

$sql2 = "SELECT ImageData FROM tbDocMgtImages WHERE DocMgtImageID IN (" . implode(',', $imageIdArray) . ")";
于 2012-12-24T21:26:13.217 回答
0

您正在将 $imageIdArray 传递到 sql 中,您应该先将其内爆

于 2012-12-24T21:25:23.243 回答
0

您可以在这样的查询中发送一个原始数组。您需要将其转换为预期的字符串,例如1,2,3,....

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

现在我假设这些不是用户输入,并且是另一个数据库中的 INTEGER 类型列,因此不需要引用,但在其他情况下,您需要确保在内爆之前转义每个值。

于 2012-12-24T21:25:54.263 回答