7

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.

3 Answers 3

9

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your answer ^^ i've tried that way. the return is not the result of the query but just an "array" text came out when i ran the code... maybe i got wrong on the controller code ??
when i try, i got an error like this "Trying to get property of non-object" maybe my code is still wrong ? ^^
It means there is no result. You can check to see if $query->num_rows() > 0 before returning the result.
Great !! it Works ! thank you very much, i really much appreciate it ^^
Glad it helped. Please accept the answer which helped you the most.
3

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

2 Comments

thank you very much for the explanation, i really much appreciate it ! ^^ it worked ^^
No worries, I'm pleased it helped.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.