Php PDO数组到字符串的转换-参数

Php PDO数组到字符串的转换-参数,php,mysql,arrays,pdo,parameters,Php,Mysql,Arrays,Pdo,Parameters,非常确定这有一个简单的解决方案,但对于PDO语句,我在数组处理方面缺乏经验 目的:从数据库中检索一组id,然后使用这些id进行比较和检索 $SAList = getSecurityActionsForQuestion("$QID"); $con = database::getInstance()->getConnection(); $ParentList = $con->prepare('SELECT SQ.SurveyQuestionID, SQ.SurveyQuestionDes

非常确定这有一个简单的解决方案,但对于PDO语句,我在数组处理方面缺乏经验

目的:从数据库中检索一组id,然后使用这些id进行比较和检索

$SAList = getSecurityActionsForQuestion("$QID");
$con = database::getInstance()->getConnection();
$ParentList = $con->prepare('SELECT SQ.SurveyQuestionID, SQ.SurveyQuestionDesc 
FROM SurveyQuestion SQ
JOIN  SurveyQuestionLookup SQLP ON SQLP.SurveyQuestionID = SQ.SurveyQuestionID
WHERE SQLP.SecurityActionID IN (:SAList)
   AND SQLP.SurveyQuestionID != :QID');
$ParentList->bindValue(':QID',$QID, PDO::PARAM_STR);
$ParentList->bindValue(':SAList',$SAList, PDO::PARAM_STR);
$ParentList->execute();
$Results = $ParentList->fetchAll();
现在,“getSecurityActionForQuestion”函数和插入的select语句分别工作并检索我想要的内容。问题是我无法将数组发送到PDO::PARAM_STR中,而且我还没有找到对此的修复方法

我已经看到了一些内爆和准备字符串的选项,但我不完全确定如何将其纳入我当前的设计中

有可能使用foreach循环来创建一个新数组,保存一个count变量,然后在SQL语句中插入一组命名参数,然后使用另一个for循环将每个参数绑定到各自的位置,但我敢肯定,有人有一个比hackjob更优雅的过程

编辑:

为清晰起见,添加了getSecurityActionsForQuestionID函数

function getSecurityActionsForQuestion($QID)
{
    $con = database::getInstance()->getConnection();
    $SAList = $con->prepare("SELECT DISTINCT SecurityActionID FROM SurveyQuestionLookup
    WHERE SurveyQuestionID = :QID");
    $SAList->bindValue(':QID',$QID,PDO::PARAM_INT);
    $SAList->execute();
    $Results = $SAList->fetchAll();
    $SAList=null;
    $con=null;  
    return $Results;
}
你能用吗?占位符而不是命名占位符,并使用值数组执行查询,而不是单独绑定每个值。请参见中的示例5


PDO::PARAM_STR是默认的绑定类型,因此它将在不指定它的情况下用于此绑定。

我喜欢此解决方案,但是否有任何方法来清理信息以尝试阻止SQL注入?我知道它是一个内部函数,所以不应该很容易被截取,但最好知道它与单独绑定值具有相同的效果,并且使用这种方法不应该否定使用准备好的语句的任何积极效果。因为您直接插入到SQL中的唯一内容是占位符,并且参数仍然是单独传递和绑定的,所以这种方式不应该增加SQL注入漏洞。这只是做同样事情的一个较短的方法。这很奇怪。我仍然收到您代码中的数组到字符串转换错误。编辑:另外,在标记中是否可以访问$placeholders,或者是否应该将其替换为$placeholders变量并将其串联?我从来没有把变量和字符串分开过,所以我不确定这是不是可以做到的,愚蠢的我,我知道。在哪条语句上?$ParentList->execute$SAList;
$SAList = getSecurityActionsForQuestion("$QID");
$con = database::getInstance()->getConnection();

// create placeholders for each item in your array
$placeholders = rtrim(str_repeat('?,', count($SAList)),',');

$ParentList = $con->prepare("SELECT SQ.SurveyQuestionID, SQ.SurveyQuestionDesc 
FROM SurveyQuestion SQ
JOIN  SurveyQuestionLookup SQLP ON SQLP.SurveyQuestionID = SQ.SurveyQuestionID
WHERE SQLP.SecurityActionID IN ($placeholders)
   AND SQLP.SurveyQuestionID != ?");

// add the $QID into the array
$SAList[] = $QID;

// execute the query with the array of values
$ParentList->execute($SAList);

$Results = $ParentList->fetchAll();