'How to fix this 'Call to a member function row() on boolean' error?
I want to fetch some data from mysql. I am using codeigniter model and controller for this.
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'];
But this error is showing :
An uncaught Exception was encountered
Type: Error
Message: Call to a member function row() on boolean
I am a beginner of codeigniter. What am i doing wrong ?
Solution 1:[1]
The error you are getting is because your $result = $q->row() is acting on a FALSE return from the $db->query(...) call.
Call to a method on boolean
I believe there is an error in your original SQL around this section ...sma_sales desc limit...
You need some sort of ORDER BY clause inbetween the sma_sales and desc.
You should also check that your returned query is valid..
if($q !== false)
{
$result = $q->row();
}
else
{
return false;
}
Or something like that
Solution 2:[2]
Here is your answer, I don't know why no matter what I did it keeps persisting. Maybe it's a bug with Codeigniter but this is how I solved it.
since you need a row of results. use the result() method of the query object, then assess the first index of the array returned like this
result()[0]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dan Streeter |
| Solution 2 | Emmanuel Aliji |
