带有 mysql 变量的 Mysql 查询在 Zend Framework 1 中不起作用

Mysql Query with mysql variable not working in Zend Framework 1(带有 mysql 变量的 Mysql 查询在 Zend Framework 1 中不起作用)
本文介绍了带有 mysql 变量的 Mysql 查询在 Zend Framework 1 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zend 配置是:resources.db.adapter = "pdo_mysql"

Zend config is : resources.db.adapter = "pdo_mysql"

得到Message: SQLSTATE[HY000]: General error

$db = Zend_Db_Table::getDefaultAdapter();
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);       


        $qry =  $db->fetchAll(" SET @rank = 0 ;
                                SELECT
                                   @rank := @rank +1 AS rank,
                                    SUM(user_scoring_data.user_points)  AS user_points,
                                    league_team.user_id

                                FROM league_team 
                                RIGHT JOIN user_scoring_data ON league_team.league_team_id = user_scoring_data.league_team_id

                                WHERE league_id = '$league_id' GROUP BY user_scoring_data.league_team_id 
                                ORDER BY user_points DESC"
                            );

如果我删除 mysql 变量,它会起作用:

if I remove the mysql variable, it works:

$db->fetchAll("
                                SELECT
                                    SUM(user_scoring_data.user_points)  AS user_points,
                                    league_team.user_id

                                FROM league_team 
                                RIGHT JOIN user_scoring_data ON league_team.league_team_id = user_scoring_data.league_team_id

                                WHERE league_id = '$league_id' GROUP BY user_scoring_data.league_team_id 
                                ORDER BY user_points DESC"
                            );

*注:查询直接在phpmyadmin中测试过,没有错误.

*Note: query was tested directly in phpmyadmin and has no errors.

推荐答案

在 MySql 中设置变量似乎被认为是一个单独的查询.( fetch*() 不能处理多个调用)

It seems setting a variable in MySql is considered a separate query. ( fetch*() cannot handle multiple calls)

所以对于那些有这种问题的人:

so for those with this kind of issue:

$db->query("SET @rank = 0 ;");

$db->fetchAll("SELECT
                                   @rank := @rank +1 AS rank,
                                    SUM(user_scoring_data.user_points)  AS user_points,
                                    league_team.user_id

                                FROM league_team 
                                RIGHT JOIN user_scoring_data ON league_team.league_team_id = user_scoring_data.league_team_id

                                WHERE league_id = '$league_id' GROUP BY user_scoring_data.league_team_id 
                                ORDER BY user_points DESC"
                            );

这篇关于带有 mysql 变量的 Mysql 查询在 Zend Framework 1 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)