[zend][db] 具有多个变量的 fetchAll
我试图在有 2 个变量的查询上使用 fetchAll 。我无法弄清楚语法。 我只能使用 1 个变量进行管理:
$sql = "SELECT * FROM mytable WHERE field1 = ?";
$this->_db->fetchAll($sql,$value1); # that works
但是,当查询有多个变量时,我遇到了一些问题
$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
我想使用的原因是?我没有将变量直接放入查询中,而是了解到使用 ?允许数据库引擎一般编译查询并提高性能。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两种类型的参数:命名参数和位置参数。你把这两种类型混合在一起是行不通的。
命名参数按名称与占位符匹配。名称以
:
符号开头。参数名称与您碰巧使用它们的列的名称不同。您可以使用参数名称(而不是列名称)作为数组键,在关联数组中提供参数值。例如:位置参数使用
?
符号作为占位符。您使用简单(非关联)数组提供参数值,并且数组中值的顺序必须与查询中参数占位符的顺序匹配。例如:大多数品牌的 SQL 数据库本身仅支持其中一种样式,但 PDO 尝试通过在准备查询之前根据需要重写 SQL 来支持这两种样式。由于 Zend_Db 是根据 PDO 建模的,因此 Zend_Db 也支持这两种参数样式。
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: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: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.
这个问题有点老了,但我想我只是添加它以供参考。
我建议开始将 Zend_Db_Select 与 Zend_Db 一起使用。我最近用 Zend_Db 做了很多事情。更多内容来自 Zend_Db_Select 参考指南。
假设您有一个 Zend_Db 适配器: $this->_db
这是给定示例的基础知识,但是选择对象上的 Zend Framework 参考指南有很多关于如何使用 JOINS、UNIONS 构建更复杂查询的好信息、GROUP BY、LIMIT、HAVING 等。
如果您想对表或参数使用别名,您可以使用关联数组,别名为索引值:
如果您只想返回选定的字段,您可以添加一个字段名称数组作为第二个参数:
实际上,上面的内容应该生成完全限定的 SQL,如下所示:
但为了简洁和清晰起见,我在示例中编写了上面的内容。
我刚刚遇到的一件事是在 WHERE 条件中使用 AND 和 OR。
请注意,无论您使用以下“where”函数,都将与前面的语句组合作为该操作数。好吧,这听起来很令人困惑。
如果第二个“where”是“OR”,则它将是“OR”条件。如果第二个“where”是“AND”,则语句将为“AND”。
换句话说,第一个 WHERE 函数在其使用的条件方面被忽略。
事实上,我昨天刚刚在 Stack Overflow 上问了一个关于执行 使用 select 的复杂 WHERE。
希望有帮助!
干杯!
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
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:
If you want to return only selected fields, you add an array of field names as a second parameter:
Actually, the above could should produce fully qualified SQL as:
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.
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!
试试这个:
$this->_db 必须是数据库适配器的实例。
Try this:
$this->_db must be an instance of Db adapter.
这是实际的 Zend 编码方式。
$sql = "SELECT * FROM mytable WHERE field1 = :param1 AND field2 = :param2";
$this->_db->fetchAll($sql,array("param1"=>$value1,"param2"=>$value2));
这对我来说非常有用
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));
This works great for me