Zend: How to use SQL query with 'like' keyword?

Viewed 24469

I am using zend framework. I am using following query in zend and it is working for me perfectly.

$table = $this->getDbTable();
$select = $table->select();
$select->where('name = ?', 'UserName');
$rows = $table->fetchAll($select);

Now I want to create another query in zend with 'like' keyword. In simple SQL it is like that.

SELECT * FROM Users WHERE name LIKE 'U%'

Now how to convert my zend code for above query?

2 Answers

Try:

$table = $this->getDbTable();
$select = $table->select();
$select->where('name LIKE ?', 'UserName%');
$rows = $table->fetchAll($select);

or if UserName is a variable:

$table = $this->getDbTable();
$select = $table->select();
$select->where('name LIKE ?', $userName.'%');
$rows = $table->fetchAll($select);
Related