错误:无法将类CI_DB_mysql_result的对象转换为字符串 [英] Error : Object of class CI_DB_mysql_result could not be converted to string

查看:139
本文介绍了错误:无法将类CI_DB_mysql_result的对象转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CodeIgniter的新手,我曾经尝试阅读CI的文档,但仍然无法解决我的问题,也许有人可以帮助解决我的问题.这是我的代码:

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:

在我的控制器中

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

在我的模型中

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

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

也许这里有人可以帮助解决我的问题? 谢谢.

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

推荐答案

您需要返回查询结果:

function ambilBeratPria($tinggi) {

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

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

     return $query->result();

}

如果结果为单行:

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

这篇关于错误:无法将类CI_DB_mysql_result的对象转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆