Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2010
    Location
    God's Own Country
    Posts
    87
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Error when working with MVC

    Hello..

    I am working with Model View Controller design pattern..I wrote a simple program,.I have created a model,a views and a controller.
    I have made a database "helloworld" and a table 'data' within it..The database details like its username,password,database name are already incorporated into a file called "database.php", which is a file in the config folder.

    My problem is I'm geting an error llike,

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: Helloworld_model::$db

    Filename: models/helloworld_model.php

    Line Number: 16

    Fatal error: Call to a member function get() on a non-object in /home/sarika/workspace/ci/system/application/models/helloworld_model.php on line 16

    helloworld_model.php contains the following code
    PHP Code:
    class Helloworld_model extends Model
       
    {  
         function 
    Helloworld_model()  
         {  
             
    // Call the Model constructor  
             
    parent::Model();  
         }  
       
         function 
    getData()  
             {  
                 
                 
    //Query the data table for every record and row  
                 
    $query $this->db->get('data');  
       
                 if (
    $query->num_rows() > 0)  
                 {  
                     
    //show_error('Database is empty!');  
                 
    }
                 
                 else
                 {  
                     return 
    $query->result();  
                 }  
             }  
     } 
    I don't know what to do to correct the error............If anyone knows how to correct it.. please help?

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    are you using codeigniter?

    either add "database" to the autoload library, or do this ~

    PHP Code:
    class Helloworld_model extends Model
       
    {  
         function 
    Helloworld_model()  
         {  
             
    // Call the Model constructor  
             
    parent::Model();  
             
    $this->load->library('database'); // load the database library.
         

    edit

    PHP Code:
     $query $this->db->get('data');  
       
                 if (
    $query->num_rows() > 0)  
                 {  
                     
    //show_error('Database is empty!');  
                 
    }
                 
                 else
                 {  
                     return 
    $query->result();  
                 } 
    doesn't strike me as being correct - if there are records, do nothing, else return an empty result object?

  • #3
    New Coder
    Join Date
    Feb 2010
    Location
    God's Own Country
    Posts
    87
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank You Met for your help..now the database problem is cleared..but as u told now the problem is with,
    return $query->result();

    That I think is manageable on my own..once again Thank You.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •