简体   繁体   English

zend框架查询中的where语句

[英]zend framework where statement in query

How can I use And/or in mysql queries using php and zend framework.for now I am using this : 我如何使用php和zend framework在MySQL查询中使用And / or。

    $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? 如果不是,正确的方法是什么?

You can add AND 's to your query by calling where() multiple times: 您可以通过多次调用where()AND添加到查询中:

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

This will translate into: 这将转换为:

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

To add one or more OR 's to your query, use orWhere() : 要将一个或多个OR添加到查询中,请使用orWhere()

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

This will translate into: 这将转换为:

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

Note 注意

Be sure to use the ? 一定要使用? placeholder syntax as it is an easy way to prevent SQL injections. 占位符语法,因为它是防止SQL注入的简便方法。

That's one way, but you should be using ? 那是一种方式,但是您应该使用? placeholders for variables to protect against SQL injection: 变量的占位符,以防止SQL注入:

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

(do the same for 'sold' if this sometimes also comes from a PHP variable.) (如果有时也来自PHP变量,则对“出售”执行相同的操作。)

You can also chain together where clauses with multiple where() calls or orWhere() if you want or instead of and: 您还可以将具有多个where()调用或orWhere() where子句链接在一起orWhere()如果需要),或者代替and:

$select=new Zend_Db_Select($db);
$select->from('users','*');
$select->joinInner('rest', 'users.repository = rest.id', array('username'));
$select->where('username = ?' $rest);
$select->where('sold = ?', 0);

Since Zend_Db_Select uses a fluent influence you can also write it like this: 由于Zend_Db_Select使用流利的影响力,因此您也可以这样编写:

$select=new Zend_Db_Select($db);
$select->from('users','*')
       ->joinInner('rest', 'users.repository = rest.id', array('username'))
       ->where('username = ?' $rest)
       ->where('sold = ?', 0);

The following will fix your question, see PHP postgres Zend Framework Select query with WHERE 'AND' clause for a more complete writeup on how where() and orWhere() work. 以下内容将解决您的问题,请参阅带有 WHERE'AND '子句的PHP postgres Zend Framework Select查询,以更全面地了解where()和orWhere()的工作方式。

$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);//multiple where() will use AND operator in query
    $select->where('sold = ?', 0);//if you need the OR operator use the orWhere() method
    return $db->fetchAll($select);

To answer about the question marks (Excerpt from manual): 要回答问号(摘录自手册):

fetchAll($select, $value)//most of the Zend_Db methods follow this API

fetchAll() method: The first argument to this method is a string containing a SELECT statement. fetchAll()方法:此方法的第一个参数是包含SELECT语句的字符串。 Alternatively, the first argument can be an object of class Zend_Db_Select. 或者,第一个参数可以是Zend_Db_Select类的对象。 The Adapter automatically converts this object to a string representation of the SELECT statement 适配器自动将此对象转换为SELECT语句的字符串表示形式

ie (? is a place holder for $value): 即(?是$ value的占位符):
$select->where('id = ?', $id);
$select->orWhere('age > ?', $age);

You are currently using the old syntax, while it will still work it has been deprecated. 您当前使用的是旧语法,但仍然可以使用,但已弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM