[zend][db] 具有多个变量的 fetchAll

发布于 2024-09-08 07:26:34 字数 542 浏览 9 评论 0原文

我试图在有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

计㈡愣 2024-09-15 07:26:34

有两种类型的参数:命名参数和位置参数。你把这两种类型混合在一起是行不通的。

命名参数按名称与占位符匹配。名称以 : 符号开头。参数名称与您碰巧使用它们的列的名称不同。您可以使用参数名称(而不是列名称)作为数组键,在关联数组中提供参数值。例如:

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

位置参数使用 ? 符号作为占位符。您使用简单(非关联)数组提供参数值,并且数组中值的顺序必须与查询中参数占位符的顺序匹配。例如:

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

大多数品牌的 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:

$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.

你曾走过我的故事 2024-09-15 07:26:34

这个问题有点老了,但我想我只是添加它以供参考。

我建议开始将 Zend_Db_Select 与 Zend_Db 一起使用。我最近用 Zend_Db 做了很多事情。更多内容来自 Zend_Db_Select 参考指南

假设您有一个 Zend_Db 适配器: $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();

这是给定示例的基础知识,但是选择对象上的 Zend Framework 参考指南有很多关于如何使用 JOINS、UNIONS 构建更复杂查询的好信息、GROUP BY、LIMIT、HAVING 等。

如果您想对表或参数使用别名,您可以使用关联数组,别名为索引值:

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

如果您只想返回选定的字段,您可以添加一个字段名称数组作为第二个参数:

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

实际上,上面的内容应该生成完全限定的 SQL,如下所示:

SELECT 'products'.model FROM 'products'

但为了简洁和清晰起见,我在示例中编写了上面的内容。

我刚刚遇到的一件事是在 WHERE 条件中使用 AND 和 OR。

# 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);

请注意,无论您使用以下“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

# 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!

试试这个:

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

$this->_db 必须是数据库适配器的实例。

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.

掀纱窥君容 2024-09-15 07:26:34

这是实际的 Zend 编码方式。

$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);

这对我来说非常有用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文