1

I want execute the query SELECT * FROM table WHERE type = 'AAA' AND id = 'BBB'.

The following code works.

$res = db_select('table', 's')
  ->fields('s')
  ->condition('type', $type)
  ->execute()
  ->fetch();

$res = db_select('table', 's')
  ->fields('s')
  ->condition('id', $id)
  ->execute()
  ->fetch();

The following code doesn't work.

$res = db_select('table', 's')
  ->fields('s')
  ->condition('type', $type)
  ->condition('id', $id)
  ->execute()
  ->fetch();

I get an error about the ID not being unique.

UPDATE:

Thanks and sorry. This code works well. I found error in another site.

  • Can you explain how the last query doesn't work, have you checked the result of writing the same query in raw SQL? – googletorp Jun 25 '11 at 17:58
  • According to your update, this has been resolved. Can you please write that as an answer or close this question so that it is marked as resolved. – Berdir Jun 25 '11 at 20:24
1

The code:

$res = db_select('table', 's')
->fields('s')
->condition('type', $type)
->condition('id', $id)
->execute()
->fetch();

is not faulty in itself. The problem is probably that you don't have a record with matches both id and type. If you want to make an or query, you need to use db_or

  • Yes. I want execute query: SELECT * FROM table WHERE type = 'AAA' AND id = 'BBB' – vitaly.batonov Jun 25 '11 at 17:21
  • what you did should work then what error do you get – googletorp Jun 25 '11 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.