HemalHerath
HemalHerath

Reputation: 1056

Pass specific id to model and take data from database using codeigniter?

My controller method like below

public function index($book_id) {

        print_r($book_id);

}

I will get the id from view. View like below

<a href="http://localhost/bookmooch/book/index/<?php echo $book->book_id; ?>"></a>

I need to get specific row according id on model how can I do that if I use query in controller its not working

If I use like below in controller it gives something else as result

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);
}

but if I use same query in model with hard coded id for testing it gives the expected out put

Please help me with this

Upvotes: 0

Views: 163

Answers (1)

Robin Rai
Robin Rai

Reputation: 392

You don't need to include "/index" in anchor tag href as controller default method will be index if found. And "print_r($book_id)" should be "echo $book_id" as $book_id is variable not array.

Please try this code (either in model/controller):

$query = $this->db->get_where('books', array('book_id' => $book_id));
$result = $query->row_array();
print_r($result);

Upvotes: 1

Related Questions