首页 > 解决方案 > 如何从原始查询中获取 sql 结果

问题描述

在 codeIgniter 中获取 SQL 结果时出错。当我在 phpmyadmin 中使用相同的查询时,我成功获得了结果。有什么问题,请帮忙。

模型:

public function calculateScore()
{
    $team_points = $this->db->query('SELECT teams.team_name,
                                        COUNT(matches.winner) AS Win
                                    FROM
                                        matches
                                    RIGHT JOIN teams ON teams.team_name = matches.winner
                                    GROUP BY teams.team_name, matches.winner
                                    ORDER BY win DESC');

    return $team_points;
}

错误:

{
    "conn_id": {
        "affected_rows": null,
        "client_info": null,
        "client_version": null,
        "connect_errno": null,
        "connect_error": null,
        "errno": null,
        "error": null,
        "error_list": null,
        "field_count": null,
        "host_info": null,
        "info": null,
        "insert_id": null,
        "server_info": null,
        "server_version": null,
        "sqlstate": null,
        "protocol_version": null,
        "thread_id": null,
        "warning_count": null
    },
    "result_id": {
        "current_field": null,
        "field_count": null,
        "lengths": null,
        "num_rows": null,
        "type": null
    },
    "result_array": [],
    "result_object": [],
    "custom_result_object": [],
    "current_row": 0,
    "num_rows": null,
    "row_data": null
}

标签: phpsqlcodeigniter

解决方案


如果您需要一个数组作为结果:

return $team_points->result_array();

或者只是标准对象:

return $team_points->result();

另外,我认为你的sql语句有错误。你打算写:

ORDER BY matches.winner

推荐阅读