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

[eluser]macleodjb[/eluser]
ok i commented it out and i get this error again.
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Joes_class::$db

Filename: libraries/Joes_class.php

Line Number: 12

Line 12 is my database line of my query


Also returns this below the error message

Fatal error: Call to a member function query() on a non-object
#12

[eluser]xzela[/eluser]
aha!
I think i got it,

Try this:
Modify your Joes_class file:
Code:
class Joes_class {
var $ci; //defines a place for the instance;    
function Joes_class() {
  $this->ci =& get_instance(); //get's an instance of the class;
  $this->ci->load->database(); //loads the database into the instances;
}    
function get_from_database() {
  $industry = "SELECT * FROM test_table";
  $result = $this->ci->db->query($industry); //call the database method query() within the instance;
  return $result->result_array(); //this will return an array;  
}
}

I would highly recommend NOT using this method of getting data from a database. You've just created a model but in a difficult way. Another way to do this would be to use the Model method:

Create a file in the /system/application/model directory called 'industry_model.php';
copy this code into that file
Code:
class Industry_model extends Model {
function Industry_model() {
  $this->load->database(); //loads the database class
}

function get_industry() {
   $query = $this->db->query('select * from industry where parentid = 0'); //run the query
   return $query->result_array(); //returns an array of data
}
}


Now in your Register controller change this line:
Code:
//$this->load->library('Joes_class');
$this->load->model('industry_model');

And to call the get_industry() method, change this line
Code:
//$industry_names = $this->joes_class->get_industry();
$industry_names = $this->industry_model->get_industry();

that should do it.

let me know if anything goes wrong.
#13

[eluser]macleodjb[/eluser]
Thanks for taking the time to help me out so thoroughly and patiently. I did what you suggested by creating the model, and i am getting the following error.

Code:
Fatal error: Call to a member function result_array() on a non-object


Here's my model file
Code:
<?php

class Register_model extends Model{

    function Register_model(){

        parent::Model();
    }

    function get_industry() {
       $query = $this->db->query('select * from industry where parentid = 0'); //run the query
       return $query->result_array(); //returns an array of data
    }

}?>

Here is my controller
Code:
<?php
class Register extends Controller {

    function Register()
    {
        parent::Controller();
        $this->CI = & get_instance();
        $this->load->database('default');
        //$this->load->library('Joes_class');
                    
    }
    

    function index()
    {
        $this->load->model('Register_model');
        $data['page_title'] = "";
        $data['page_desc'] = "";
        $data['page_keywords'] = "";
        
        $data['industry'] = $this->Register_model->get_industry();
        $this->load->view('account/register_form', $data);
        
    }
    

}

?>

let me know if i've totally screwed this up. I'm glad to read other posts that other people are struggling just as i am.
#14

[eluser]xzela[/eluser]
You almost have it! Smile

In your controller file, comment out the line to load the database class
Code:
function Register()
    {
        parent::Controller();
        $this->CI = & get_instance();
        //$this->load->database('default');
        //$this->load->library('Joes_class');
                    
    }

Re-add it in the constructor function within your model.

Your model constructor function should look something like this:
Code:
function Register_model(){
        parent::Model();
        $this->load->database('default'); //this loads the database class within the model
    }

Now the get_industry() function will be able to access the database class.

Let me now if you have any other issues.
#15

[eluser]macleodjb[/eluser]
You got my hopes up. I thought i actually was getting somewhere. But no. I still get the same error.

Why is it have database loaded in my autoload file, i have to also reload it in my model?
#16

[eluser]xzela[/eluser]
This is so bizarre, I just copied your model and controller code verbatim and it works.
That particular error message is stating that the database class is not being loaded correctly. Which is the original issue if I remember correctly.

hmmm... there are a few things that can cause the database library from loading, and one of the major contributors are improper installations.

What does your $config['base_url'] look like?
(it's located in the /system/application/config/config.php file)

what if you changed it to this:
Code:
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

This is a shot it the dark but it may do something...

good luck!
#17

[eluser]macleodjb[/eluser]
wow, there sure don't outline this in the user docs.

my config file shows this
Code:
$config['base_url']    = "http://www.example.com/";

I really appreciate you taking the time to troubleshoot for me. This is frustrating me beyond belief.

edit:your patch didn't affect my problem. Do you think this is a config problem or perhaps a server problem?
#18

[eluser]xzela[/eluser]
hmmm... good question, do you have a local copy on your workstation or is it only up on your server?

I would guess it is a configuration problem, because as long as you're using apache1.5/2 and php5 you should be good to go. By the way are you using MySQL or something else?
#19

[eluser]macleodjb[/eluser]
I've got it working now. I deleted all files and unpacked it again. Redid my one model and it works fine now. Jesus this was exhausting. I write the pages locally and then upload to my server.

I've got it now, but thank you very very much for having the patience to sit through my frustrations.

thanks again.
#20

[eluser]xzela[/eluser]
good to hear it's working now. That's a relief.

no problem for the help, that's what these forums are for. Smile

good luck on your site.




Theme © iAndrew 2016 - Forum software by © MyBB