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.

2
  • Can you explain how the last query doesn't work, have you checked the result of writing the same query in raw SQL?
    – googletorp
    Commented Jun 25, 2011 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
    Commented Jun 25, 2011 at 20:24

1 Answer 1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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