Call to a member function result() on boolean in CodeIgniter, SQL
Solution 1
You stored result in $data['results']
and print $results
so how can it work?
print $data['results']
as below
print_r($data['results']); die;
You can use $results
in view.
Solution 2
I faced the same issue, my application was working fine until 2 days ago i started getting the same error. figured out the query was returning no data at all and got the same error you mentioned. Somehow i found a solution which worked for me. I had to edit the mysqli driver i was using in CI.
At Line num 147 /public_html/system/database/drivers/mysqli/mysqli_driver.php I changed the below code
`$this->_mysqli->options(MYSQLI_INIT_COMMAND,
'SET SESSION sql_mode =
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
@@sql_mode,
"STRICT_ALL_TABLES,", ""),
",STRICT_ALL_TABLES", ""),
"STRICT_ALL_TABLES", ""),
"STRICT_TRANS_TABLES,", ""),
",STRICT_TRANS_TABLES", ""),
"STRICT_TRANS_TABLES", "")'
);`
To this Code
`$this->_mysqli->options(MYSQLI_INIT_COMMAND,
'SET SESSION sql_mode =
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
@@sql_mode,
"ONLY_FULL_GROUP_BY,", ""),
",ONLY_FULL_GROUP_BY", ""),
"ONLY_FULL_GROUP_BY", ""),
"ONLY_FULL_GROUP_BY,", ""),
",ONLY_FULL_GROUP_BY", ""),
"ONLY_FULL_GROUP_BY", "")'
);`
And make sure the striction
is set to FALSE
in your database.php file under /application/config/ folder. Like this 'stricton' => FALSE,
Related videos on Youtube
Stuti Rauthan
Updated on June 04, 2022Comments
-
Stuti Rauthan almost 2 years
I'm trying to see what data is coming in $data['results'] on basis of search keyword but getting above mentioned fatal error every time, can somebody help me with it. My Controller
public function execute_search() { $search_term = $this->input->post('search'); $data['results'] = $this->UserModel->get_results($search_term); print_r($results); die; //$this->load->view('search_result',$data); }
My Model:
public function get_results($search_term) { //var_dump($search_term);die; $this->db->select('*'); $this->db->from('Competitor_Products'); $this->db->where('CProduct_Article_Number', $search_term); return $this->db->get()->result(); }
-
Anand Pandey over 6 yearswhat is the error you faced?
-
Narf over 6 yearsThat error means the query has failed.
-
Stuti Rauthan over 6 years@Narf When running same query in sql getting perfect result, only here in execution error is occuring.
-
Narf over 6 yearsHere you're making a bunch of function calls; you can't be sure that you're executing the same query. Call
get_compiled_select()
to get the query being generated.
-
-
DFriend over 6 yearsWrong. A model should return data not a
CI_DB_result
instance. -
DFriend over 6 years@romanreign, the error message is in the title to this question.
-
Devsi Odedra over 6 yearsThan first check there is record or not $query->num_rows() > 0
-
Alex over 6 years@DFriend not wrong, just another way of doing things. The best example I can think of is if you need to get say
result()
, andnum_rows()
(often required). Instead of having to do acount()
on result, you can simply do$this->users->groups()->num_rows()
and$this->users->groups()->result()
I would argue its as maintainable as returning an object and its DRY because you don't have to have a separate function and db call to getnum_rows
. The Ion Auth plugin for CI does this extensively. -
DFriend over 6 yearsIt is wrong as it is not the answer to the question. As to my assertion that models should return results instead of a db connection I'll agree that it's a matter of preference - to a certain extent. Ion_auth_model does this extensively? I think not. Look again. Maybe once. A couple times it returns $this which is not the same. Mostly it returns bool or void. As to DRY, I cannot think of a single occasion where I needed to know the number of records retrieved outside of the model.
-
Jawad A. over 6 years@StutiRauthan maybe the search term you are using doesn't get any result from database. When there is no result, the get() function gives BOOLEAN FALSE; hence the error is right, you cant call result() on a function when there is no data returned, remove result(), and try this one,
if($this->db->get()->num_rows() > 0) { return $this->db->get()->result(); } else { die("no data recieved"); }
-
masarapmabuhay over 3 yearsYou can also verify using the following command in the MySQL Window: REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''); Additional Reference: stackoverflow.com/questions/23921117/disable-only-full-group-by; answer by breq, 20160516T0713; edited 20180226T0746