Zend:如何使用带有“like”关键字的 SQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1921394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:46:23  来源:igfitidea点击:

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

sqlzend-framework

提问by Naveed

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

我正在使用 zend 框架。我在 zend 中使用以下查询,它对我来说非常有用。

$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.

现在我想用 'like' 关键字在 zend 中创建另一个查询。在简单的 SQL 中就是这样。

SELECT * FROM Users WHERE name LIKE 'U%'

Now how to convert my zend code for above query?

现在如何为上述查询转换我的 Zend 代码?

回答by Justin Swartsel

Try:

尝试:

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

or if UserName is a variable:

或者如果 UserName 是一个变量:

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

回答by SagarPPanchal

$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);