PHP MySQL PDO选择具有id的数组上的行

时间:2016-02-11 16:31:37

标签: php arrays select pdo

我在会话中从表单[]中存储了一些多个id值。

我尝试在mysql pdo数据库查询中使用这些id,如下所示:

$noot = str_repeat("?,", count($_SESSION['uniqueID'])-1) . "?";

$printen = 1;
$query = $db->prepare("SELECT DISTINCT ru.GB,g.Name,g.Address,g.Place,ri.Ricode,lo.nr,lo.uniqueID,GROUP_CONCAT(ru.control SEPARATOR '<BR>') AS control,GROUP_CONCAT(ru.BestEmail SEPARATOR '<BR>') AS email,GROUP_CONCAT(ru.ont_info SEPARATOR '<BR>') AS ont,GROUP_CONCAT(ru.sup SEPARATOR '<BR>') AS Lev,GROUP_CONCAT(ru.OrderID SEPARATOR '<BR>') AS orderID,GROUP_CONCAT(DISTINCT ru.rID SEPARATOR '<BR>') AS RID,GROUP_CONCAT(ru.Pack SEPARATOR '<BR>') AS packet FROM rutrans AS ru JOIN routes AS ri ON ru.rID = ri.rID 
 LEFT OUTER JOIN lock AS lo
             ON ri.Ricode = lo.ricode AND ru.GB = lo.location
    LEFT OUTER JOIN gbc AS g
             ON ru.GB = g.CodeB          
WHERE  lo.uniqueID IN ($noot)");

$query->execute($_SESSION['uniqueID']);

//$data = $query->fetchAll();

尝试得到这样的结果:

foreach($queryRT as $rowRT) { 

 (I echo database results within a table for each row here,... or try to do this)

}

发生以下错误:

  

致命错误:带有消息的未捕获异常'PDOException'   'SQLSTATE [HY093]:

中的参数号无效

当我添加

$data = $query->fetchAll();

查询页面完全空白。

当我尝试这样的事情时:

print_r($data);

页面上仅显示以下内容:

  

Array()

当我尝试这样的事情时:

print_r($_SESSION['uniqueID']);

它显示如下数组:

Array ( [B1601222016164345] => B1601222016164345 
        [B1601222016161121] => B1601222016161121 
        [B1601262126474345] => B1601262126474345 
        [B1602011856554345] => B1602011856554345 
        [B1602101921434345] => B1602101921434345 
        [Z1602102002034345] => Z1602102002034345
        [Z1602102026544345] => Z1602102026544345 
      )

显然我做错了。 是否有人有想法解决这个问题?

1 个答案:

答案 0 :(得分:3)

如果您使用$_SESSION['uniqueID']作为占位符,

?需要为其索引编号。

Docs

  

input_parameters中的键必须与SQL中声明的键匹配。在PHP 5.2.0之前,它被默默地忽略了。

试试这个:

$query->execute(array_values($_SESSION['uniqueID']));