错误:类CI_DB_mysql_result的对象不能转换为字符串


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

我是新来的CodeIgniter,我试着阅读CI的文档,但我仍然无法解决我的问题,也许这里有人可以帮助解决我的问题。下面是我的代码:

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

在我的模型

function ambilBeratPria($tinggi) {
    $this->db->select('berat')->from('pria')->where('tinggi',$tinggi);
    $query = $this->db->get();
    return $query;           
}

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

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

也许这里有人能帮我解决这个问题?谢谢。

您需要返回查询的结果:

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

当前您正在尝试直接回显$this->db->get();返回的值。但是,要使用查询的结果,您需要生成结果。

如果生成如下查询:

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

然后有几个生成结果的选项。这些示例假设在返回的行中有一列称为weight


result() -允许您将结果用作对象的数组。

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() -允许您将结果用作数组

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 ($query->num_rows() > 0)
{
   $row = $query->row();   
   echo $row->weight;
}

row_array() -与row()相同,但允许您使用数组

if ($query->num_rows() > 0)
{
   $row = $query->row_array(); 
   echo $row['weight'];
}

当我在一个PHP项目中使用Code Igniter框架时,我得到了同样的错误。在我的模型类中有一个方法叫做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();
                                    }

我想在控制器类中调用这个函数,并在视图类的下拉列表中使用它。

所以在控制器类中,

   $this->data['projects'] =$this->testcase_model->getprojectnames();
   $this->load->view("admin/_layout_main",$this->data);

在view类中

<?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();?>

当我运行这个时,我得到了错误,告诉CI_DB_mysql_result不能转换为字符串。因此,我通过更改模型类中的代码来解决这个问题,如下所示:

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