Welcome Guest, Not a member yet? Register   Sign In
Help with database connection
#1

[eluser]manton[/eluser]
Dear All
Please help me with the database connection. I decided to use CI, but it seems more difficult for me than connecting manually with pure PHP.
I already set up the database config in the database like this
Code:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "pass";
$db['default']['database'] = "products";
$db['default']['dbdriver'] = "mysql";

I have one table called products, and I made a model for that table here's the code:
Code:
<?php
class MProducts extends Model {

    function MProducts()
    {
        parent::Model();
    }
}

function getAllProducts(){
    $data = array();
    $Q = $this->db->get('products');
    if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
            $data[] = $row;
        }
    }
    $Q->free_result();
    return $data;
    }
and I put it MProducts and also database library in autoload.php and then I have a welcome controller here's the code:
Code:
<?php

class Welcome extends Controller {

    public function __construct()
    {
        parent::Controller();    
    }
    
    function index(){
        $data['title'] = "Welcome to My Shop";
        $data['navlist'] = $this->MProducts->getAllProducts(); [color=red]//line 12[/color]
        $this->load->vars($data);
        $this->load->view('template');
    }
When I run them with the view template I get an error like this:
Fatal error: Call to undefined method MProducts::getAllProducts() in C:\xampp\htdocs\testci\system\application\controllers\welcome.php on line 12
I've been around this problem for 2 days, and wondering what's wrong with my code now I decided I need help. Please help
#2

[eluser]Rey Philip Regis[/eluser]
try

Code:
$this->mproducts->getAllProducts();
#3

[eluser]therealmaloy[/eluser]
you need to load your model in your controller to be able to access and/or use it.
#4

[eluser]manton[/eluser]
Hi Guys thanks for reply,
Rey, I have done your suggestion, the error is becoming undefined property like this:
Undefined property: Welcome::$mproducts

Hi therealmaloy, first I have put the model in autoload, however I'd done your suggestion, the error is still the same.

I still need help.., or maybe there is a bug in CI 1.7 on database connection or autoload.php?
#5

[eluser]invi[/eluser]
Maybe it's trival but I had similar problem. I don't know exactly if this is a solution but I have changed the name of the Model with only the First capital letter. So maybe you can try change it to Mproducts and the same with function call and besides that you had better load the model inside the Controller.
#6

[eluser]therealmaloy[/eluser]
manton,

i might be wrong.. but i guess if the posted code is really from your model.. check the closing brace of your model Smile I am about to check/run your code but kind of lazy at the moment and suddenly i noticed this, just try this and let us know.

the class was accidentally closed Smile

<?php
class MProducts extends Model {

function MProducts()
{
parent::Model();
}
}<-------------- this is the problem, this must be moved on the end part of the class

....
#7

[eluser]manton[/eluser]
Hi therealmaloy

You are my hero.., thanks a lot my friend. I owe you one...

:-)

Problems Solved... Hooray!!
#8

[eluser]therealmaloy[/eluser]
:cheese:

you're welcome




Theme © iAndrew 2016 - Forum software by © MyBB