Codeigniter Mysql Query is not performed

I want to make a simple search engine that looks for data from database and show them up, but I can't figure why the queries are performed here is my code :

Model Page

class Reference_model extends CI_Model{


        function search($match){
            $this->db->where('sReference', $match);
            $this->db->like('nReference', $match);
            $this->db->or_like('sSearch', $match);
            $this->db->from('treference', $match);
            $query = $this->db->get();

                if($query->num_rows > 0){
                    $output = '<ul>';
                    foreach ($query->result() as $rows){
                        $output .= '<li>'.$rows->nReference.'</li>';
                    }
                    $output .='</ul>';
                }else{
                    return "<p>No Results!</p>"; //No Results!
                }
                return $output;
    }
}

Controller Page

class References extends CI_Controller {


    function index()
    {   
        $q = $this->input->post('q');
        $this->load->model('Reference_model');
        $results['result'] = $this->Reference_model->search($q);


        $this->load->view('constants/header');
        $this->load->view('references',$results);
        $this->load->view('constants/footer');

}
}

when I perform the search from the search form I get the No Results!, can some one help me out, because I've tried with many times without, and I'm not a professional webdeveloper I'm just learning, thank all the contributors

Upvotes: 1

Views: 61

Answers (3)

reignsly
reignsly

Reputation: 502

Change this

if($query->num_rows > 0){

to

if($query->num_rows() > 0){

The num_rows should be a function. If still doesnt work. Try to dump the query being generated and post it here.

var_dump($this->db->last_query()); die('x'); if($query->num_rows > 0){

Upvotes: 1

David Coder
David Coder

Reputation: 1138

Model :

    class Reference_model extends CI_Model
    {


        function search($match)
        {
            $this->db->select('*');   //this line
            $this->db->where('sReference', $match);
            $this->db->like('nReference', $match);
            $this->db->or_like('sSearch', $match);
            $this->db->from('treference');  //this line
            $query = $this->db->get();

                if($query->num_rows > 0){
                    $output = '<ul>';
                    foreach ($query->result() as $rows){
                        $output .= '<li>'.$rows->nReference.'</li>';
                    }
                    $output .='</ul>';
                }else{
                    return "<p>No Results!</p>"; //No Results!
                }
                return $output;
        }
    }

Try to debug your query with echo /print_r OR echo $this->db->last_query();

Hope this will help you.

Upvotes: 1

kapigsaan
kapigsaan

Reputation: 64

$this->db->like('sReference', $match);
$this->db->like('nReference', $match);
$this->db->or_like('sSearch', $match);

and this part

$this->db->from('treference', $match);

change it to

$this->db->from('treference');

try this..

hope it helps..

Upvotes: 1

Related Questions