zend框架查询中的where语句 [英] zend framework where statement in query

查看:246
本文介绍了zend框架查询中的where语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在使用php和zend framework的mysql查询中使用和/或.现在我正在使用它:

How can I use And/or in mysql queries using php and zend framework.for now I am using this :

    $db=Zend_Db_Table::getDefaultAdapter();
    $select=new Zend_Db_Select($db);
    $select->from('users','*');
    $select->joinInner('rest', 'users.repository = rest.id',array('username'));
    $select->where('username='.$rest.' and sold=0');
    return $db->fetchAll($select);

这是正确的方法吗?如果不是正确的方法是什么?

is that the correct way ? if not what is the correct way?

推荐答案

您可以通过多次调用where()来将AND添加到查询中:

You can add AND's to your query by calling where() multiple times:

$select->where('this = ?', 'myValue')
       ->where('that = ?', 'myValue2');

这将转换为:

... WHERE this = 'myValue' AND that = 'myValue2'

要在查询中添加一个或多个OR,请使用orWhere():

To add one or more OR's to your query, use orWhere():

$select->where('this = ?', 'myValue')
       ->orWhere('that = ?', 'myValue2');

这将转换为:

... WHERE this = 'myValue' OR that = 'myValue2'

注意

请确保使用?占位符语法,因为这是防止SQL注入的简便方法.

Be sure to use the ? placeholder syntax as it is an easy way to prevent SQL injections.

这篇关于zend框架查询中的where语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆