gpt4 book ai didi

php - 如何修复此 'Call to a member function row() on boolean' 错误?

转载 作者:行者123 更新时间:2023-12-02 03:21:30 25 4
gpt4 key购买 nike

我想从mysql中获取一些数据。我为此使用了 codeigniter 模型和 Controller 。

CI_model.php:

     public function getLastSale($id){


$q = $this->db->query("SELECT * from sma_sales desc limit 1 where customer_id = '$id' ");


$result = $q->row();


$res = array();
$res['id'] = $result->id;
$res['paid'] = $result->paid;


return $res;


}

CI_controller.php:

    $getLastData = $this->pos_model->getLastSale($customer_id);

$sid = $getLastData['id'];

$prepaid = $getLastData['paid'];

但是显示此错误:

    An uncaught Exception was encountered
Type: Error

Message: Call to a member function row() on boolean

我是codeigniter的初学者。我做错了什么?

最佳答案

您收到的错误是因为您的 $result = $q->row() 正在对来自 $db->query(...)< 的 FALSE 返回进行操作 调用。

调用 bool 值方法

我相信您的原始 SQL 中关于此部分的错误 ...sma_sales desc limit...

您需要在 sma_salesdesc 之间使用某种 ORDER BY 子句。

您还应该检查返回的查询是否有效。

if($q !== false)
{
$result = $q->row();
}
else
{
return false;
}

或者类似的东西

关于php - 如何修复此 'Call to a member function row() on boolean' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54614921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com