Mysql Query with mysql variable not working in Zen

2019-02-20 04:01发布

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

the query that gets 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"
                            );

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

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

1条回答
祖国的老花朵
2楼-- · 2019-02-20 04:27

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"
                            );
查看更多
登录 后发表回答