如何在codeigniter 4中获取最后一个查询?

t1qtbnec  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(126)

这是我目前的代码:

public function index()
{
    $tbl = 'm_trx';
    $db = \Config\Database::connect();
    $builder = $db->table($tbl);
    
    $data = [];

    $cols = "{$tbl}.*";
    $builder->select($cols);

    $data = $builder->get()->getResult();
    $asd = [
        'message' => $builder->getLastQuery(),
        'data' => $data,
    ];
    return $this->respond($asd);
}

但它返回错误
"message": "Call to undefined method CodeIgniter\\Database\\MySQLi\\Builder::getLastQuery()",
如果我将$builder->getLastQuery()更改为$builder->db->getLastQuery(),它将返回此错误
"message": "Cannot access protected property CodeIgniter\\Database\\MySQLi\\Builder::$db",

svmlkihl

svmlkihl1#

您只需要传递$db,而不需要传递$builder->db
试试这个

$db->getLastQuery()

相关问题