[英]Call to undefined method CI_DB_mysqli_result::where() in CodeIgniter
我正在嘗試使用 CodeIgniter 通過鍵“系列”和“id”拉取照片來設置路線。 它將遵循以下邏輯:
照片/ :返回數據庫中的所有表“照片”。
照片/系列:返回匹配某個系列的所有照片。 'photos/nature' 將返回鍵 'series' 等於 'nature' 的所有照片。
photos/series/id : 返回單張照片。 'photos/nature/1' 將返回一張照片,其中鍵 'series' 等於 'nature','id' 等於 '1'
調用 ' http://localhost/public_html/photos/nature/1 ' 我收到以下錯誤:
遇到 PHP 錯誤
嚴重性:錯誤
消息:調用未定義的方法 CI_DB_mysqli_result::where()
文件名:models/Photos_model.php
行號:14
回溯:
我的“照片”表結構:
CREATE TABLE `photos` (
`filename` varchar(45) NOT NULL,
`series` varchar(45) NOT NULL,
`id` int(11) NOT NULL,
`caption` varchar(45) NOT NULL,
`description` varchar(45) NOT NULL,
PRIMARY KEY (`filename`))
“照片”表的值:
INSERT INTO `photos` VALUES
('file00053809264.jpg','nature',1,'waterfall','description of waterfall'),
('file000267747089.jpg','nature',2,'forest','description of burning forest'),
('file000325161223.jpg','cloudburst',1,'sky','description of sky'),
('file000267804564.jpg','cloudburst',2,'bursting abstract','description bursting abstract'),
('file000132701536.jpg','landmarks',1,'taj mahal','description taj mahal');
我在 CodeIgniter 中的照片控制器:
<?php
class Photos extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('photos_model');
$this->load->helper('url_helper');
$this->load->helper('html'); // needed for header
$this->load->helper('url'); // needed for footer
}
public function view($series = NULL, $id = NULL) {
$data['photos'] = $this->photos_model->get_photos($series, $id)->result_array();
if (empty($data['photos'])){
show_404();
}
$data['title'] = 'Photos';
$this->load->view('templates/header', $data);
$this->load->view('photos/view', $data);
$this->load->view('templates/footer');
}
public function index() {
$data['photos'] = $this->photos_model->get_photos();
$data['title'] = 'Photos';
$this->load->view('templates/header', $data);
$this->load->view('photos/index', $data);
$this->load->view('templates/footer');
}
}
我的照片模型:
<?php
class Photos_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_photos($series = FALSE, $id = FALSE) {
if ($series === FALSE && $id === FALSE) {
$query = $this->db->get('photos');
} else if ($id === FALSE) {
$query = $this->db->get_where('photos', array('series' => $series));
} else {
$query = $this->db->get('photos')->where('series', $series)->where('id', $id);
}
return $query->result_array();
}
}
我的“照片/view.php”視圖:
<?php
echo $photos['photos'];
我的路由配置:
$route['photos/'] = 'photos/view';
$route['photos/(:any)'] = 'photos/view/$1';
$route['photos/(:any)/(:num)'] = 'photos/view/$1/$2';
如果你能告訴我我在哪里以及如何滑倒,你能給我解釋一下嗎? 謝謝你。
根據在get()
where()
之后調用where()
錯誤可能會導致此問題作為替代移動get()
之前的所有where()
調用或使用get_where()
$query = $this->db
->where('series', $series)
->where('id', $id)
->get('photos');
或者
$query = $this->db->get_where('photos', array('series' => $series,'id'=>$id));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.