简体   繁体   English

WHERE子句中的PHP INT数组-数组到字符串的转换

[英]PHP INT Array In WHERE Clause - Array To String Conversion

  • Using PHP and SQLSRV Driver 使用PHP和SQLSRV驱动程序

I have to display binary images from an SQL Server database based on patient ID. 我必须根据患者ID显示来自SQL Server数据库的二进制图像。 The patient IDs and images are located in two different databases. 患者ID和图像位于两个不同的数据库中。

From the first db I query for image IDs based on the Patient ID entered, and add the results into an array(). 在第一个数据库中,我根据输入的患者ID查询图像ID,然后将结果添加到array()中。 Then I would like to use this array of IDs to get the images from the second database. 然后,我想使用此ID数组从第二个数据库获取图像。

Problem: I get the following error when using the array in the WHERE caluse of my sql statement: 问题:在我的sql语句的WHERE原因中使用数组时,出现以下错误:

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

That is because you are trying to interpret the array as a string. 那是因为您试图将数组解释为字符串。 You need to explictly make this conversion to string by doing something like: 您需要执行类似以下操作来明确地将此转换为字符串:

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

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

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,... . 您需要将其转到所需的字符串中,例如1,2,3,...

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

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. 现在,我假设这些不是用户输入的,并且是另一个DB中的INTEGER类型的列,因此不应该使用引号,但是在其他情况下,您需要确保在内爆之前对每个值进行转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM