2009-12-17 26 views
16

Tôi đang sử dụng khung công tác zend. Tôi đang sử dụng truy vấn sau trong zend và nó đang làm việc cho tôi một cách hoàn hảo.Zend: Làm thế nào để sử dụng truy vấn SQL với từ khoá 'thích'?

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

Bây giờ, tôi muốn tạo một truy vấn khác bằng từ khóa 'like'. Trong SQL đơn giản, nó là như thế.

SELECT * FROM Users WHERE name LIKE 'U%' 

Bây giờ làm cách nào để chuyển đổi mã zend của tôi cho truy vấn trên?

Trả lời

41

Hãy thử:

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

hoặc nếu UserName là một biến:

$table = $this->getDbTable(); 
$select = $table->select(); 
$select->where('name LIKE ?', $userName.'%'); 
$rows = $table->fetchAll($select); 
0
$user = new Application_Model_DbTable_User(); 
// User List 
$uname=$_POST['uname']; 

$query = $user 
    ->select() 
    ->where('firstname LIKE ?', $uname.'%') 
    ->ORwhere('lastname LIKE ?', $_POST['lname'].'%') 
    ->ORwhere('emailid LIKE ?', $_POST['email'].'%'); 

$userlist = $user->fetchAll($query); 
Các vấn đề liên quan