• Register
1 vote
493 views

Problem :

I am beginner to CodeIgniter, If I try to execute my code then I face below error

“Object of class ci_db_mysqli_result could not be converted to string”.

Please find below my code for your reference.

class MyReg extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->model('registration_model','rmod');
    }

    function myambil() {

        $mygender = $this->input->post('kelamin');  

        $mytinggi = $this->input->post('height'); 

        $myberat  = $this->input->post('weight');

        $myweight = $this->rmod->ambilBeratPria($mytinggi);

        echo $myweight;
    }

Please find below code for my model

function ambilBeratPria($mytinggi) {

    $this->db->select('berat')->from('pria')->where('mytinggi',$mytinggi);

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

    return $myquery;           
}

I am trying to get a result of my above query in my model but I often face above error.

I am looking for CodeIgniter experts help in fixing above error.

9 6 2
3,980 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution :

I have experience in CodeIgniter so trying to help you. I can see in your code you want to directly echo your value returned by following line of code $this->db->get();. But I can tell you to use the result from your query you first need to generate the results.

If you know how to generate the query as show below:

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

Then you can have many options to generate results. Below example take for granted that you have the column in your row being returned called the myweight.

result() : It will allow you to use your results as the array of objects.

if ($myquery->num_rows() > 0)  //Please ensure that there is at least one result

{

   foreach ($myquery->result() as $row)  //Need to iterate through results

   {

      echo $row->myweight;

   }

}

 

5 4 2
17,150 points

Related questions

0 votes
1 answer 182 views
182 views
Problem: I have only basic knowledge on PHP. I am currently facing a problem with PHP. I am facing this error: Object of class stdClass could not be converted to string. Above error occurs as soon as I try to run below portion of PHP code from my site: function ... : $data['user_data'] = $this->auth->get_userdata($this->uri->segment(3)); I am looking for correct guidance on my above PHP error.
asked Sep 1, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 14 views
14 views
Problem: Please help me fix this dilemma: message: object of class ci_loader could not be converted to string
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 51 views
51 views
Problem: object of class datetime could not be converted to string.
asked Feb 23 Ethan ross 2.7k points
0 votes
0 answers 8 views
8 views
Problem: Hi Can any one help me ? My computer is showing this and I am unable to fix it?
asked Mar 17 Nomio555 24.6k points
0 votes
1 answer 4 views
4 views
Problem: I need assistance in resolving this problem of Catchable fatal error: object of class wp_error could not be converted to string in?
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 522 views
522 views
Problem: I want to add some of the data to my database but I am receiving below error: Catchable fatal error: Object of class PDOStatement could not be converted to string in /var/www/mandje.php on line 114. This is the code which is causing above error: foreach($_SESSION ... ->execute($sql); I am unable to find out my mistake. Looking for a PHP expert who can guide me in fixing the above error.
asked Aug 21, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 40 views
1 vote
1 answer 313 views
313 views
Problem: I am very new to the coding. I want to echo a result of my mysqli_query but I keep on facing below error: 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'. Please tell me the way ... SQLi query should be printed on the screen, but above error is getting generated instead. Please guide me in fixing above mysqli related error.
asked Jun 4, 2020 Martin K 6.6k points
0 votes
1 answer 41 views
41 views
Problem: trying to get property of non-object in codeigniter
asked Feb 14 Sabbir Hossain 14.6k points