How do bindValue in PHP when the value is an array?

Asked

Viewed 224 times

0

I’m trying to pass more than an id to search in my program but the search only brings an object with the data of the first id, query code in sql:

    public static function listar_servidores_ids($id){
    //LISTA SERVIDORES SELECIONADOS

    $pdo    = \Core\DB::getConnection();
    $sql    = 'SELECT * FROM servidores WHERE id IN (?)';
    $stmt   = $pdo->prepare($sql);
    $stmt->bindValue(1, $id);
    $stmt->execute();

    return $stmt-> fetchAll(\PDO::FETCH_OBJ);
}

(When I pass through the ids to the inves of "?" in the query it occurs normally, but when I pass for example '1.2'instead of $id in the bindValue only returns that of the first id, same thing as when I pass the ids via url Example:"http://localhost:8080/index? id/[1,4,3]") Controller code that receives the id and calls the query function in the database:

public function execute()
{
    //CHAMA A OPERAÇÃO DE ALTERAR OS DADOS UM SERVIDOR
    $ids = Input::args('id');
    $ids = str_ireplace("[", "", $ids);
    $ids = str_ireplace("]", "", $ids);
    $this->set( 'servidor',    \App\Operacoes::listar_servidores_ids( $ids ) );

}

1 answer

-1

If I understand your question, I believe you should return your query by making a foreach.

Example:

Array (id => 1)

foreach ($array as $key => $value){
   $type = (is_numeric($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
   $stmt->bindValue(":{$key), $value, $type);
}

It will go through all arrays separating the index, value and type for the BindValue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.