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?
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.
To answer about the question marks (Excerpt from manual):
fetchAll($select, $value)//most of the Zend_Db methods follow this API
ie (? is a place holder for $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.
You can add
AND
's to your query by callingwhere()
multiple times:This will translate into:
To add one or more
OR
's to your query, useorWhere()
:This will translate into:
Note
Be sure to use the
?
placeholder syntax as it is an easy way to prevent SQL injections.That's one way, but you should be using ? placeholders for variables to protect against SQL injection:
(do the same for 'sold' if this sometimes also comes from a PHP variable.)
You can also chain together where clauses with multiple
where()
calls ororWhere()
if you want or instead of and:Since Zend_Db_Select uses a fluent influence you can also write it like this: