Error: object of class CI_DB_mysql_result cannot be converted to a string

I'm new to CodeIgniter, I tried to read the CI documentation, but I still can’t solve my problem, maybe someone here can help fix my problem. Here is my code:

In my controller

class Registration extends CI_Controller{ function __construct(){ parent::__construct(); $this->load->model('registration_model','rmod'); } function ambil() { $gender = $this->input->post('kelamin'); $tinggi = $this->input->post('height'); $berat = $this->input->post('weight'); $weight = $this->rmod->ambilBeratPria($tinggi); echo $weight; } 

In my model

 function ambilBeratPria($tinggi) { $this->db->select('berat')->from('pria')->where('tinggi',$tinggi); $query = $this->db->get(); return $query; } 

I want to get the result of my query in the model, but I get this error:

Message: Object of class CI_DB_mysql_result could not be converted to string

Can someone here help solve my problem? Thanks.

+7
source share
3 answers

You need to return the result of the query:

 function ambilBeratPria($tinggi) { $this->db->select('berat')->from('pria')->where('tinggi',$tinggi); $query = $this->db->get(); return $query->result(); } 

EDIT:

If the result is a single line:

 function ambilBeratPria($tinggi) { $this->db->select('berat')->from('pria')->where('tinggi',$tinggi); $query = $this->db->get(); if ($query->num_rows() > 0) { return $query->row()->berat; } return false; } 
+8
source

You are currently trying to directly retell the value returned by $this->db->get(); . However, to use the result from the query, you need to generate the results.

If you create a query as follows:

 $query = $this->db->get(); 

Then there are several options for generating results. These examples assume that you have a column in the row returned with the name weight.


result() . Allows you to use the results as an array of objects.

 if ($query->num_rows() > 0) //Ensure that there is at least one result { foreach ($query->result() as $row) //Iterate through results { echo $row->weight; } } 

result_array() . Allows you to use the results as an array.

 if ($query->num_rows() > 0) //Ensure that there is at least one result { foreach ($query->result_array() as $row) //Iterate through results { echo $row['weight']; } } 

row() . If you expect only one result, this may be helpful. If the query generates multiple results, only the first row is returned. Allows you to use the result as an object.

 if ($query->num_rows() > 0) { $row = $query->row(); echo $row->weight; } 

row_array() - Same as row() , but allows you to use the result as an array.

 if ($query->num_rows() > 0) { $row = $query->row_array(); echo $row['weight']; } 
+2
source

I got the same error when I was working in a PHP project with the Code Igniter framework. In my model class there was a getprojectnames () method,

 public function getprojectnames(){ $result['names']=array(); $this->db->select('name'); $this->db->from('project'); $this->db->where('status','Not Completed'); $query=$this->db->get(); return $query->result(); } 

I wanted to call this function in the controller class and use it in the drop-down list in the view class.

So in my controller class

  $this->data['projects'] =$this->testcase_model->getprojectnames(); $this->load->view("admin/_layout_main",$this->data); 

In my class class,

 <?php echo form_open('admin/check1'); ?> <div class="row" style=" margin-left: 10px; margin-top: 15px;"> <h5 class="box-title">Projects List &nbsp;&nbsp;&nbsp; : &nbsp; <? php echo form_dropdown('projects', $projects, 'id'); ?> </h5> <div> <input type="submit" style="width:200px;" class="btn btn-block btn-primary" value="Show Project TestCase Status" /></div> </div> <?php echo form_close();?> 

When I ran this, I got an error saying that CI_DB_mysql_result could not convert to String.So I solved this problem by changing my code in the model class as shown below,

 public function getprojectnames(){ $result['names']=array(); $this->db->select('name'); $this->db->from('project'); $this->db->where('status','Not Completed'); $query=$this->db->get(); foreach ($query->result() as $row) { array_push($result,$row->name); } return $result; } 

Then my program worked fine.

+1
source

All Articles