简体   繁体   English

如何在Zend中获取查询结果

[英]How to fetch the result of a query in Zend

I need to get the avg score in a variable. 我需要在一个变量中获得平均分。 I am using zend and I have tried two queries and none of them are working: 我正在使用zend,并且尝试了两个查询,但没有一个在工作:

$avg_query = $db->query("SELECT sid, AVG(score) 
                         FROM markstable GROUP BY sid");

$avg_query = $db->query("SELECT sid, AVG(score) 
                         FROM markstable WHERE sid = '$sid'");

$avg_score = $db->fetchAll($avg_query);
echo $avg_score; gives nothing
echo $avg_score['$sid']; gives nothing

I need to store the avg_score in some other table. 我需要将avg_score存储在其他表中。

You can use this to fetch avg grouped by sid. 您可以使用它来获取按sid分组的平均值。

$avg_score = $db->fetchAll("SELECT sid,AVG(score) FROM st GROUP BY sid");

foreach($avg_score as $row) {
    echo $row["sid"]."  ".$row["AVG(score)"];
}

In case of second query : 如果第二次查询:

$avg_score = $db->fetchRow("SELECT sid,AVG(score) FROM st where sid=1");
echo $avg_score["AVG(score)"];

Try fetchPairs() instead of fetchAll() 尝试使用fetchPairs()而不是fetchAll()

Edit: Zend_Db does have a fetchPairs(); 编辑:Zend_Db确实有一个fetchPairs();。

http://framework.zend.com/manual/1.12/en/zend.db.adapter.html#zend.db.adapter.select.fetchpairs http://framework.zend.com/manual/1.12/zh/zend.db.adapter.html#zend.db.adapter.select.fetchpairs

Also remove quotes from round the variable in the array index 同时删除数组索引中的变量的引号

echo $avg_score[$sid];

You mixed syntax in your attempt to query the DB. 您尝试查询数据库时混合了语法。

The query() method builds and executes the desired query. query()方法构建并执行所需的查询。 Your first query should return the result, no need to provide any further code: 您的第一个查询应返回结果,无需提供任何其他代码:

$avg_query = $db->query("SELECT sid, AVG(score) FROM markstable GROUP BY sid");
Zend_debug::dump($avg_query);//same as var_dump but already includes <pre> tags

your second query needs to include any bound values in an array as the second argument: 您的第二个查询需要在数组中包含所有绑定值作为第二个参数:

$avg_query = $db->query("SELECT sid, AVG(score) FROM markstable WHERE sid = ?", array($sid);
Zend_debug::dump($avg_query);//same as var_dump but already includes <pre> tags

you can also execute this query using a standard fetch method, just pass in the SQL as a string: 您还可以使用标准的提取方法执行此查询,只需将SQL作为字符串传递:

$avg_score = $db->fetchAll("SELECT sid, AVG(score) FROM markstable GROUP BY sid");
Zend_debug::dump($avg_score);//same as var_dump but already includes <pre> tags

when dealing with Zend_Db components you may as well get used to using placeholders in your queries ( ? ) as the old style syntax has been deprecated in many of the components. 在处理Zend_Db组件时,您可能还习惯于在查询( ? )中使用占位符,因为许多组件已弃用了旧式语法。

This is probably not the best solution to what you're doing. 这可能不是您正在做的事情的最佳解决方案。 It does however answer your question. 但是,它确实回答了您的问题。 There are a rather large number of ways to interact with a database in Zend Framework and using Zend_Db_Adapter components and methods are typically not the best way to accomplish the task. 与Zend Framework中的数据库进行交互的方式有很多,使用Zend_Db_Adapter组件和方法通常不是完成任务的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM