2013-04-09 71 views
0

我收到上述錯誤,而我試圖想從我的媒體庫訪問功能顯示在...笨:致命錯誤:調用一個成員函數select()的非對象

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class My_crud { 

public function select_from($select, $from) { 
    // dafaults 
    $output = ""; 
    // querying 
    $this->db->select($select); 
    $this->db->from($from); 
    $query = $this->db->get(); 
    // if no rows returned 
    if ($uqery->num_rows == 0) { 
     return $output = "No Results Found"; 
    } 
    // if row(s) retunred 
    return $output = $query->result_array(); 
} 
} 

database庫設置爲自動加載。

+0

擴展類後,我得到這個錯誤''致命錯誤:類'CI_model'找​​不到'' – 2013-04-09 02:11:54

+0

看看[文檔](http://ellislab.com/codeigniter/user-guide/ general/models.html)並檢查您的大小寫。 – 2013-04-09 02:27:54

回答

3

如果你是在一個庫中創建它,你應該得到CI的一個實例,你可以在類變化this->db中添加它在你__construct

class My_crud 
{ 
var $ci; 
public function __construct() 
{ 
    $this->ci =& get_instance(); 
} 
} 

然後在你的方法$this->ci->db,問題這就是爲什麼你可以在模型中製作一個圖書館時對圖書館產生影響?

0

你的課需要擴展一些東西。現在設置的方式是$this->db正在尋找不存在的My_crud類中的$db參數。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class My_crud extends CI_Model { 

    public function select_from($select, $from) { 
     // defaults 
     $output = "No Results Found"; 

     // querying 
     $query = $this->db 
      ->select($select) 
      ->get($from); 

     if ($query->num_rows() > 0) 
      $output = $query->result_array(); 

     return $output; 
    } 
} 
+0

現在我得到這個致命錯誤:找不到'CI_model'類' – 2013-04-09 02:09:21

+0

他指定了庫,而不是模型。圖書館不擴展模型。 – 2013-04-09 02:29:41

+0

如果他正在製作一個CRUD模型,我會建議擴展CI_Model而不是製作一個庫。 – 2013-04-09 02:30:53

相關問題