简体   繁体   中英

PHP MySQL PDO selecting rows on array with id's

I stored some multiple id values from a form[] in a session.

I tried to use these id's within a mysql pdo database query as follow:

$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();

And try to get the results like this:

foreach($queryRT as $rowRT) { 

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

}

The following error occures:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number' in

When I add

$data = $query->fetchAll();

to the query the page is totally blank.

When I try something to show like this:

print_r($data);

Only the following is being displayed on the page:

Array ( )

When I try something like this:

print_r($_SESSION['uniqueID']);

It shows the array as follow:

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

Obviously I am doing something totally wrong. Is there anybody who would have an idea to solve this problem?

$_SESSION['uniqueID'] needs to have numbers for its index if you are using ? as placeholders.

Docs

The keys from input_parameters must match the ones declared in the SQL. Before PHP 5.2.0 this was silently ignored.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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