0

Whith help from a Question I ask for some Minutes I get from this code:

$uid = $_SESSION['uid'];
$sth = $db->prepare("SELECT groupid  FROM user_groups WHERE userid=?");
$sth->execute(array($uid));
$sth->execute();

$results = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
$results = implode(",",$resultas);
echo $results;

A Output like this: 11,13. Thanks for your Help:)

Now I want to use this variable in another pdo-Query and I try this:

$stmt = $db->query('SELECT * FROM menu WHERE gruppe=0 OR gruppe in (?'
    . ') ORDER BY reihe, parentId, name ');
$stmt->execute(array($results));

But there is no result... but wehen I try this

$stmt = $db->query('SELECT * FROM menu WHERE gruppe=0 OR gruppe in (11,13'
    . ') ORDER BY reihe, parentId, name ');
$stmt->execute();

Isn't this the same? Can you pleas give me another hint for this?

Albzi
  • 15,431
  • 6
  • 46
  • 63
Ingo
  • 27
  • 1
  • Note that it is only a duplicate if you need to do it in 2 separate queries. I voted to close as it was an unreadable mess in the beginning :-) – jeroen Apr 28 '15 at 15:44

1 Answers1

2

You should do this in one query as you would need to bind all values individually otherwise:

SELECT * FROM menu
  WHERE gruppe=0 OR gruppe IN (SELECT groupid FROM user_groups WHERE userid=?)
  ORDER BY reihe, parentId, name
jeroen
  • 91,079
  • 21
  • 114
  • 132