Search for list of ids in mysql via php

Asked

Viewed 55 times

0

Does anyone know how to get multiple elements from a database using an array that contains all the search id?? PHP/Mysql

static function list($ids){
    $wa = "";
    foreach ($ids as $key => $value) {
      $wa .= " e_produto.id = {$value} OR";
    }
    $wa = substr($wa,0,-2);
    $wa = ($wa!="")?"OR $wa":"";
    ////------------
    $myid = User::id();
    $query = "SELECT * FROM tabela WHERE $wa;";
    $retorno = DB::dbQuery($query,$carrinhoArr);

    return $retorno;
  }

It’s the provisioning solution I got. But there is a "more correct way"?

  • You could give an example of mysql code?

2 answers

1

You can use this way at the time of consultation.

$ids = array('1', '2', '3');
$query = "SELECT * FROM users WHERE id IN (" . implode(', ', $ids) . ")";

// Saída
SELECT * FROM users WHERE id IN (1, 2, 3)

0

Browser other questions tagged

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