[zend][db] fetchAll with multiple variables

2019-05-03 12:12发布

I'm trying to use fetchAll on a query that has 2 variables. I can't figure out the syntax. I can manage with only 1 variable:

$sql = "SELECT * FROM mytable WHERE field1 = ?";
$this->_db->fetchAll($sql,$value1);  # that works

However I'm having some issues when query has multiple variables

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

The reason why I want to use ? instead of placing the variables directly into the query is that I've learned that using ? allows for the query to be compiled generically by the db engine and improves performances.

4条回答
男人必须洒脱
2楼-- · 2019-05-03 12:22

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.

查看更多
Viruses.
3楼-- · 2019-05-03 12:23

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

查看更多
一夜七次
4楼-- · 2019-05-03 12:25

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!

查看更多
姐就是有狂的资本
5楼-- · 2019-05-03 12:27

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.

查看更多
登录 后发表回答