0

I'm trying to execute a SQL query directly from Model in CI 4

Controller:

$homemodel = new \App\Models\home_model();

$data['list'] = $homemodel->myquery();

Model:

 public function myquery(){

  $query = "SELECT id FROM tab_anagr WHERE var = 1";
        
  $query=$this->db->query($query);
      
  return $query->result_array();
                
 }   

Does'nt work and I receive this error:

Call to undefined method CodeIgniter\Database\MySQLi\Result::result_array()

I would like to receive the array for the view.

4
  • I would like to use "free" queries in my models. Commented Nov 23, 2020 at 13:59
  • "free" queries means?? Commented Nov 23, 2020 at 14:01
  • I would like to use raw code "SELECT a1, a2 FROM tab1 WHERE x = a" ect ect Commented Nov 23, 2020 at 14:12
  • that's all in the docs Generating Query Results - Result Arrays Commented Nov 23, 2020 at 18:19

1 Answer 1

4

You should use $query->getResultArray() to get the result as an array, if you want it as object you can use $query->getResult()

Sign up to request clarification or add additional context in comments.

1 Comment

$sql = "SELECT * FROM some_table WHERE id IN ? AND status = ? AND author = ?"; $db->query($sql, [[3, 6], 'live', 'Rick']);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.