Welcome Guest, Not a member yet? Register   Sign In
Error connecting to Database via class
#1

[eluser]macleodjb[/eluser]
Hi guys,

I created my own class and within the class i have some functions that query my database. I try to execute my query for the first time and i received this error.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Joes_class::$db

Filename: libraries/Joes_class.php

Line Number: 7

Should i be loading my database somehow into this class? I have it loaded in the config file.
#2

[eluser]xzela[/eluser]
I'm not sure what your class looks like, but you should try something like this:

Code:
Class Joes_class {

  function Joes_Class() {
   $this->load->database(); //this loads the database class
  }

  function test_database() {
   $this->db->query('SELECT * FROM table');
   $query = $this->db->get();

  }
}
#3

[eluser]macleodjb[/eluser]
I wish i could, now for some reason i'm getting this error message.

Code:
A Database Error Occurred

Unable to connect to your database server using the provided settings.

Now i can tell you with 100% certainty that i have the correct information for my database connector.

I'm starting to wanna throw out codeigniter and go to cakephp

Another thing...why do i have to load my database like you suggested when it is in the autoload file under libraries? Something is definitely NOT working right.
#4

[eluser]macleodjb[/eluser]
So far I've been working with codeigniter for 3 days. I've read the tutorials and the examples. Thus far, not a single thing has worked properly. For instance the .htaccess file, i copied it directly off the tutorial, doesn't work. I had to rely on this forum for someone who has a similar problem to fix it. So far i'm not seeing the benefit to using codeigniter. I've gotten one page completed since installing it and i can't continue now because i can't get to my database. It's a joke. I'm not re-inventing the wheel, i'm just trying to migrate an existing website, that was working properly.

Please shed some light on me, show me how this framework helps you?

Forgive my frustrations, i sincerely would like some help before i just throw this out and leave my site the way it was.
#5

[eluser]macleodjb[/eluser]
ok i managed to get my database to connect.

Now i have this error.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$joes_class

Filename: account/register_form.php

Line Number: 32

Did i forget to declare something here?
#6

[eluser]xzela[/eluser]
It's possible you may have forgotten to load the class.

try adding this to the function within your (register_form) controller:
Code:
$this->load->library('/path/to/Joes_Class');

then use this to access it:
Code:
$this->joes_class->hello_world();
#7

[eluser]macleodjb[/eluser]
now i get this error

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Joes_class::$db

Filename: libraries/Joes_class.php

Line Number: 8


This is what's inside my class

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Joes_class {
    
    function get_industry() {
    $industry = "select * from industry where parentid = 0";
    $result = $this->db->query($industry);
    return $result;
    }



}?>

This is whats in my controller

Code:
class Register extends Controller {

    function Register()
    {
        parent::Controller();
        $this->load->database('default');
        $this->load->library('Joes_class');            
    }
    
    function index()
    {
        $data['page_title'] = "";
        $data['page_desc'] = "";
        $data['page_keywords'] = "";
        
        $this->load->view('account/register_form', $data);
        
    }
}

and this is how i am using it in my view page.

Code:
$industry_names = $this->joes_class->get_industry();
#8

[eluser]xzela[/eluser]
Ok, i see the issue

Your class does not have a constructor
Add this as your first function:
Code:
function Joes_class() {
$this->load->database(); //this is optional, but it will load the database class
}

That should help with your class
#9

[eluser]macleodjb[/eluser]
ok i did that...and got this error now.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Joes_class::$load

Filename: libraries/Joes_class.php

Line Number: 7

line 7 is the line we just added to load the db
#10

[eluser]xzela[/eluser]
OK, lets debug one problem at a time,
to verify that your class (library) is being loaded correctly comment out this line:
Code:
function Joes_class() {
   //$this->load->database(); //this is optional, but it will load the database class
}




Theme © iAndrew 2016 - Forum software by © MyBB