-1

I'm trying to fetch multiple ids on database, but PDO is only returning result from the first id. I'm suspecting PDO has a problem with the comma.

Edit: on mysql this query is working normal, but not on PDO.

$resources = getResourcesByID("2,3,4,5");
function getResourcesByID($id)
{
    $PDO = getconnection();
    if ($PDO !== NULL) {
        $sql = "SELECT * FROM `resources` WHERE id IN (:id);";
        $stmt = $PDO->prepare($sql);
        $stmt->bindParam(":id", $id, PDO::PARAM_STR, 255);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    return array();
}
maik
  • 73
  • 4

1 Answers1

0

PDO bindParam cannot bind multiple values to one named parameter. Try IN(??) or adding multiple named parameters and adding them with stmt->bindParam individually or in a loop.

The parameter section of the manual details this a bit. https://www.php.net/manual/en/pdostatement.execute.php

dvnc0
  • 19
  • 3