加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 错误:类CI_DB_mysql_result的对象无法转换为字符串

发布时间:2020-12-13 18:03:31 所属栏目:PHP教程 来源:网络整理
导读:我是CodeIgniter的新手,我试过阅读CI的文档,但我仍然无法解决我的问题,也许有人可以帮我解决问题.这是我的代码: 在我的控制器中 class Registration extends CI_Controller{ function __construct(){ parent::__construct(); $this-load-model('registratio
我是CodeIgniter的新手,我试过阅读CI的文档,但我仍然无法解决我的问题,也许有人可以帮我解决问题.这是我的代码:

在我的控制器中

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

我想在模型中获得我的查询结果,但是我得到这样的错误:

消息:类CI_DB_mysql_result的对象无法转换为字符串

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

您需要返回查询结果:
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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读