Home » Php » php – Error : Object of class CI_DB_mysql_result could not be converted to string

php – Error : Object of class CI_DB_mysql_result could not be converted to string

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m new to CodeIgniter, I’ve tried to read the documentation of CI 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 an error like this:

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

Maybe someone here can help to solve my problem ?
Thanks.

How to&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 row:

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;
}

Answer:

Currently you’re trying to directly echo the value returned by $this->db->get();. However, to use the result(s) from the query, you need to generate the results.

If you generate the query like this:

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

Then there are several options for generating results. These examples assume that you have a column in the row(s) being returned called 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’re expecting only one result then this can be useful. If the query generates multiple results, then 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() – The 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'];
}

Answer:

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

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 a 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 view 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 run this I got the error,telling CI_DB_mysql_result could not convert into String.So I solved this problem by changing my code in the model class like as follows,

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.