0

I'm using https://github.com/Orange-OpenSource/YACassandraPDO as PDO extension, all works fine, but when i try to get records by clause select * from users Where key IN ($my_keys_array) just retrieves the first record.

$query = "SELECT * FROM questions WHERE user_id = 'mikko' AND question_id IN ('Question 1','Quesiton 2')";
$stmt = $this->db->prepare($query);
$stmt->execute ();
$res = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($res);

Somebody knows if there is an issue with cassandra-pdo or where i'm wrong?

I try the same sentence by cqlsh> prompt and works fine.

Thanks in advance for any help.

jezuz
  • 413
  • 2
  • 5
  • 13
  • If `$my_keys_array` _is_ an PHP array, you need to turn it into something SQL understands. Try: `"'" . implode("', '", $my_keys_array) . "'"` instead. What this will do is join the elements from the array as thus: `'One', 'Two', 'Three'` and that should essentially work. So your query would be: `"SELECT * FROM users WHERE key IN ('" . implode("', '", $my_keys_array) . "')"` – ninty9notout Aug 19 '13 at 15:40
  • i have tried that, even i try send "SELECT * FROM questions WHERE user_id = 'mikko' AND question_id IN ('Question 1','Question 2')" but doesn't works... – jezuz Aug 19 '13 at 15:44

2 Answers2

1

At the moment you are fetching only single row from a result set, to fetch all rows you should issue $stmt->fetchAll () instead of $stmt->fetch().

Btw. a good place to look at, how to use the API is always the tests: these can give quite a lot of information https://github.com/Orange-OpenSource/YACassandraPDO/tree/master/tests.

You can also check the PDO documentation for fetch and fetchAll.

lpiepiora
  • 13,659
  • 1
  • 35
  • 47
  • While this certainly works, this does nothing to explain why his current code doesn't. You should elaborate on this answer. – Mike Brant Aug 19 '13 at 15:48
1

Currently, you are only fetching a single row from the result set. You need to run that fetch in a loop in order to get all results from the result set.

$result_array = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $result_array[] = $row;
}
echo json_encode($result_array);
Mike Brant
  • 70,514
  • 10
  • 99
  • 103