2013-04-10 77 views
7

我是CodeIgniter的新手,我试过阅读CI的文档,但仍然无法解决我的问题,也许有人在这里可以帮助解决我的问题。这里是我的代码:错误:CI_DB_mysql_result类的对象无法转换为字符串

在我的控制器

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

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

回答

8

您需要返回查询结果:

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

感谢您的回答^^ 我试过这种方式。返回不是查询的结果,但只是一个“数组”文本出来时,我跑的代码... 也许我错了控制器代码? – 2013-04-10 17:02:12

+0

请看我编辑的答案。 – 2013-04-10 17:05:48

+0

当我尝试,我有这样的错误“试图获得非对象的属性” 也许我的代码仍然是错误的? ^^ – 2013-04-10 17:15:38

2

目前你想直接呼应由$this->db->get();返回的值。但是,要使用从查询结果(S),你需要generate the results.

如果产生这样的查询:

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

再就是产生结果的几个选项。这些示例假定您在返回的行中有一列,名为,重量为


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

非常感谢你的解释,我真的很感激它! ^^它的工作^^ – 2013-04-10 17:29:27

+0

不用担心,我很高兴它有帮助。 – jleft 2013-04-10 17:53:26

1

我得到了同样的错误,当我用代码点火器framework.In出现在我的模型类的PHP项目工作有一个方法叫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); 

在我看来类,

<?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不能转换成String.So我解决了这个通过在模型类中更改我的代码,如下所示,

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; 

             } 

然后我的程序w没问题。

相关问题