Search code examples
phpdatabasecodeigniterxampp

Call to a member function select() on a non-object : codeigniter over XAMPP


Below are my settings and code.

model.php

<?php 
class Opinion_poll_model extends CI_Model 
{ 
     public function __construct() 
     { 
         $this->load->database(); 
     } 

     public function total_votes() 
     { 
        $query = $this->db->select('COUNT(choice) as choices_count')->get('js_libraries');
        return $query->row()->choices_count; 
     } 

      public function get_results() 
      { 
        $libraries = array("", "JQuery", "MooTools", "YUI Library", "Glow"); 
        $table_rows = ''; 

        for ($i = 1; $i < 5; $i++) 
        {
            $sql_stmt = "SELECT COUNT(choice) choices_count FROM js_libraries WHERE choice = $i;"; 
            $result = $model->select($sql_stmt); $table_rows .= "<tr><td>" . $libraries [$i] . " Got:</td><td><b>" . $result[0] . "</b> votes</td></tr>"; 
         } 
     
       } 


   public function add_vote($choice) 
   { 
        $ts = date("Y-m-d H:i:s"); $data = array('choice' => $choice, 'ts' => $ts); $this->db->insert('js_libraries', $data); 
}

}
?>

database.php

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'opinion_poll';
$db['default']['dbdriver'] = 'mysql';

***Exposing passwords since its just a sample app.

I have manually created a opinion_poll database and table js_libraries through phpmyadmin.

I guess I am facing some mixup in db configurations of xampp and codeigniter. So i am getting above error.

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: model

Filename: models/opinion_poll_model.php

Line Number: 23


Fatal error: Call to a member function select() on a non-object in C:\xampp\htdocs\ciopinionpoll\application\models\opinion_poll_model.php on line 23

Solution

  • You are using $model which is not defined in the scope.

    Try using $this->db->select($sql_stmt);