2

How can I set this query

SELECT city_id FROM cities WHERE city_name LIKE "%Vicenza%"

using the Zend_Db_Select class?

Egidio Caprino
  • 553
  • 10
  • 18

2 Answers2

10

Since @Jerec's answer didn't mention it... in order to have the effect of:

LIKE '%{$searchTerm}%'

You'll need to add the extra modifiers to the variable like such:

// Correct way
->where("city_name LIKE ?", "%{$searchTerm}%")

// Wrong ways
->where("city_name LIKE %?%", $searchTerm)
->where("city_name LIKE '%?%'", $searchTerm)

Might seem obvious, but took me three attempts to get it right.

rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
2

You could use this method

   $select = $dbTable->select()
    ->from('cities', 'city_id')
    ->where('city_name LIKE ?', $searchTerm);

where $dbTable is an instance of an Zend_Db_Table class

Jerec TheSith
  • 1,932
  • 4
  • 32
  • 41