3

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();
}
Stuti Rauthan
  • 41
  • 1
  • 1
  • 4

10 Answers10

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.

Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
1

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,

  • You can also verify using the following command in the MySQL Window: REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''); Additional Reference: https://stackoverflow.com/questions/23921117/disable-only-full-group-by; answer by breq, 20160516T0713; edited 20180226T0746 – masarapmabuhay Jul 29 '20 at 02:11
0

Remove the result() from return statement as follows: 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(); }

Jawad A.
  • 73
  • 6
  • 1
    Wrong. A model should return data not a `CI_DB_result` instance. – DFriend Nov 20 '17 at 17:38
  • @DFriend not wrong, just another way of doing things. The best example I can think of is if you need to get say `result()`, and `num_rows()` (often required). Instead of having to do a `count()` 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 get `num_rows`. The Ion Auth plugin for CI does this extensively. – Alex Nov 22 '17 at 00:30
  • It 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. – DFriend Nov 22 '17 at 03:40
  • @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"); } ` – Jawad A. Nov 22 '17 at 10:06
0

I suggest you to Check on the following basic things first,this might help you...

Are you getting correct string for search

echo $search_term = $this->input->post('search');

Is $this->UserModel is correctly pointing to the model (File name conventions followed)

You also can make use of the following to check whether your query is building correctly

Echo query before execution and without execution in codeigniter Active Record

/* SELECT */ $this->db->_compile_select();
/* INSERT */ $this->db->_insert();
/* UPDATE */ $this->db->_update();
Mangesh Sathe
  • 1,987
  • 4
  • 21
  • 40
0

You have to change in print_r()

public function execute_search()
{
    $search_term = $this->input->post('search');
    $data['results'] = $this->UserModel->get_results($search_term);
    print_r($data['results']); die;
}
Anand Pandey
  • 2,025
  • 3
  • 20
  • 39
0

This is probably because CI is not correctly escaping the query's where values. It's hard to know for sure without seeing what $search_term is. You can easily see if this is the problem by changing this line

$this->db->where('CProduct_Article_Number', $search_term);

To this

$this->db->where('CProduct_Article_Number', $search_term, FALSE);

That will turn off escaping of variables and identifiers.

DFriend
  • 8,869
  • 1
  • 13
  • 26
0

I had the similar issue, but the solution was simple for me. I know its too late but can help others who face the same issue.

if($this->db->get() === false){
 return false;
}
return $this->db->get()->result();
0

This picture is for Controller

Problem occure when you forget to pass the parameter after load the model example you want to load model contain the method that have the parameterized to perform some functionality you will do like this on Controller

    <?php
class example extends CI_Controller{
public function send(){
    $this->load->model('modelname'); //call the model name
    $this->modelname->methodName($variable); //variable contain data that need to be saved on database
}
}
    ?>

This picture is for Model

After parameterized the method contain $variable data in order to access database model will work on this staff. So you must parameterized the method of model. Example you will write your code like this.

<?php
class modelname extends CI_Model{
    public function methodName($email){
        $this->db->set('password', $this->input->post('npass'));
        $this->db->where('email', $email;
        $query = $this->db->update('test');

        if($query->Result()){
            return true;
        }else{
            return false;
        }
    }
}
}
?>

I hope some of thess examples will help you to solve the error like that

Nimes
  • 303
  • 2
  • 6
0

Replace the following line

$search_term = $this->input->post('search');

with

$search_term = (isset($this->input->post('search')) && !empty($this->input->post('search'))) ? $this->input->post('search') : '';
Farhan
  • 253
  • 2
  • 9
0

You can also verify using the following command in the MySQL Window:

mysql> REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''); 

Thank you! Peace.

Additional References:

  1. Call to a member function result() on boolean in CodeIgniter, SQL answer by Gourav Bassi, 20181023T1536
  2. Disable ONLY_FULL_GROUP_BY answer by breq, 20160516T0713; edited 20180226T0746
masarapmabuhay
  • 466
  • 1
  • 9
  • 15