2013-04-10 138 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沒問題。

相關問題