我的控制器方法如下
public function index($book_id) {
print_r($book_id);
}
我将从视图中获取ID。如下视图
<a href="http://localhost/bookmooch/book/index/<?php echo $book->book_id; ?>"></a>
我需要根据模型上的id获取特定的行,如果我在控制器中使用查询不起作用怎么办
如果我在控制器中像下面那样使用它,结果将给出其他结果
public function index($book_id) {
print_r($book_id);
$this->db->select('*');
$this->db->from('books')->where('book_id', $book_id);
$query = $this->db->get();
print_r($query);
}
但是,如果我在具有硬编码ID的模型中使用相同的查询进行测试,则会给出预期的结果
请帮助我
答案 0 :(得分:1)
您不需要在锚标记href中包括“ / index”,因为控制器的默认方法是找到索引。并且“ print_r($ book_id)”应为“ echo $ book_id”,因为$ book_id是变量而不是数组。
请尝试此代码(在模型/控制器中):
$query = $this->db->get_where('books', array('book_id' => $book_id));
$result = $query->row_array();
print_r($result);