[zend][db] fetchAll with multiple variables

冷暖自知 提交于 2019-12-05 10:27:10

There are two types of parameter, named parameters and positional parameters. You're mixing the two types and that won't work.

Named parameters match a placeholder by name. Names are started with the : symbol. The parameter names are not the same as the names of the columns you happen to use them for. You supply parameter values in an associative array, using the parameter name (not the column name) as the array keys. For example:

$sql = "SELECT * FROM mytable WHERE field1 = :param1 AND field2 = :param2";
$this->_db->fetchAll($sql,array("param1"=>$value1,"param2"=>$value2));

Positional parameters use the ? symbol for the placeholder. You supply parameter values using a simple (non-associative) array, and the order of values in the array must match the order of parameter placeholders in your query. For example:

$sql = "SELECT * FROM mytable WHERE field1 = ? AND field2 = ?";
$this->_db->fetchAll($sql,array($value1,$value2));

Most brands of SQL database natively support only one style or the other, but PDO attempts to support both, by rewriting the SQL if necessary before preparing the query. Since Zend_Db is modeled after PDO, Zend_Db also supports both parameter styles.

jmbertucci

This question is a bit old, but I thought I'd just add to it for reference sake.

I would recommend starting to use Zend_Db_Select with Zend_Db. I've been doing a lot with Zend_Db lately. More from Zend_Db_Select reference guide.

Lets assume you have a Zend_Db adapter: $this->_db

# this will get the Zend_Db_Select object
$select = $this->_db->select();

# now you build up your query with Zend_Db_Select functions
$select->from('mytable');
$select->where('field1 = ?', $field1);
$select->where('field2 = ?', $field2);
[...]

# echo to see the SQL (helps in debugging)
# SELECT * FROM mytable WHERE field1 = ? AND field2 = ? [...]
echo '<p>My SQL: ' . $select . '</p>';

# Execute the SQL / Fetch results
$results = $select->query()->fetchAll();

That's the basics from your given example, but the Zend Framework reference guide on the select object has a lot of good information on how to build even more complex queries with JOINS, UNIONS, GROUP BY, LIMIT, HAVING, etc.

If you wanted to use an alias name for a table or parameters, you use an associative array with the alias name being the index value:

# SELECT p.* FROM products AS p
$select->from('p' => 'products');

If you want to return only selected fields, you add an array of field names as a second parameter:

# SELECT model FROM products
$select->from(products, array(model));

Actually, the above could should produce fully qualified SQL as:

SELECT 'products'.model FROM 'products'

but I wrote the above for brevity and clarity in the example.

One thing I just came across is using AND and OR in the WHERE condition.

# WHERE a = $a
$select->where('a = ?', $a);

# WHERE a = $a AND b = $b
$select->where('a = ?', $a);
$select->where('b = ?', $b);

# WHERE a = $a OR b = $b
$select->where('a = ?', $a);
$select->orWhere('b = ?', $b);

# WHERE a = $a AND b = $b
$select->orWhere('a = ?', $a);
$select->where('b = ?', $b);

Notice, that whatever the following "where" function you use, will combine with the previous statement as that operand. Ok, that sounded confusing.

If the second "where" is an "OR" it will be an "OR" conditional. If the second "where" is a "AND" the statement will be "AND".

In other words, the first WHERE function is ignored in terms of what condition it will use.

In fact, I just asked a question on Stack Overflow yesterday regarding doing a complex WHERE using select.

Hope that helps! Cheers!

Try this:

$sql = "SELECT * FROM mytable WHERE field1 = ? AND field2 = ?";
$statement = $this->_db->query($sql,array("field1"=>$value1,"field2"=>$value2));
$data = $statement->fetchAll();

$this->_db must be an instance of Db adapter.

Heres the actual Zend way to code for this.

$sql = "SELECT * FROM mytable WHERE field1 = :param1 AND field2 = :param2"; $this->_db->fetchAll($sql,array("param1"=>$value1,"param2"=>$value2));

$where = $this->_db->select()
->from('mytable')
->where('field1 = ?',$value1)
->where('field2 = ?',$value2);

$rowSet = $this->_db->fetchAll($where);

This works great for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!