Loading database kills my code

#1
[eluser]Unknown[/eluser]
Hey there!
I started using CI a few days ago, so i'm not totally used to it.
Anyway...i'm having some problems with the database. (MySQL) It works fine when i'm not using CI, but when i try to use it, the thing just doesn't work.
I tried to build the DB code in a Controller and then in a Model, and none worked.
This code only outputs the first string, but not the second one.

Code:
class SomeClass extends Controller {

    function SomeClass(){
        parent::Controller();
        
    }
    function index(){
        echo "first";
        $this->load->database();
        echo "second";
    }
}

Second example, using SomeClass again:

Code:
function index(){
        $this->load->model('somemodel');
    }
now, SomeModel:

Code:
class SomeModel extends Model{
    function SomeModel(){
        parent::Model();
        echo "This is from the model.";
        $this->load->database();
        echo "This is from the model too, but it doesn't work.";
    }
}
Same thing happens. It outputs the first string, but not the second.

Code:
class SomeModel extends Model{
    function SomeModel(){
        parent::Model();
        echo "This is from the model.";
        $this->db->query('SELECT name, title FROM my_table');
    }
}
This outputs the string, and gives an error:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: SomeModel::$db
Filename: models/somemodel.php
Line Number: 6

Fatal error: Call to a member function query() on a non-object in C:\Arquivos de programas\EasyPHP 2.0b1\www\ign\system\application\models\somemodel.php on line 6

I even tried to do something like this:
$db = $this->load->database();

but didn't work.

How can i solve this?

#2
[eluser]chejnik[/eluser]
Hello, have a look in config directory and see the database.php
There you store your database settings.

Then have a look into autoload.php in the same directory and add database to array of libraries
that will load in the beginning ( so you dont have to care about loading db).

Then call from controller view a model like this

Code:
function updateWord()
    {
        $this->load->model('Settings_model');
        
        $structure = $this->Settings_model->selectStructure($this->session->userdata('i'));

    }

And in model named settings_model.php this function in model directory

Code:
function selectStructure($i)
    {
      $tableSettings ='databaseSettings'.$i;  

        $this->db->select('databaseRowName');
    $this->db->from($tableSettings);
    $query = $this->db->get();
      return $query->result_array();
    }
Hope that helps little bit. Ales

#3
[eluser]Belou[/eluser]
Hi ,
I have the same problem after the database connection , the program is killed but the program is used by command line ( with a web navigator it works) .
I load a model with a manual configuration :
Code:
$this->load->model('mssql_model','data',$config);
Then the model :
Code:
function mssql_model()
    {
        parent::Model();
        $config['dbprefix'] = '';
        $config['pconnect'] = false;
        $config['db_debug'] = true;
        $config['cache_on'] = false;
        $config['cachedir'] = '';
        $config['char_set'] = 'utf8';
        $config['dbcollat'] = 'utf8_general_ci';
    
           $this->load->database($config);
               echo "yaaaaaaaaaa";
    }
But in command line i don't watch yaaaaaaaa on my screen and the databas loading close my prompt whereas with a web navigator it works.
Can you help me .
Thanks a lot
Belou

#4
[eluser]Unknown[/eluser]
You need to configure from your config/autoload.php file

Add some code..

$autoload['libraries'] = array();
$autoload['libraries'] = array('database', 'session', 'xmlrpc');


..Maybe that can help..

- mike

#5
[eluser]Belou[/eluser]
hi mikehacker ,
I think the autoload library can't solve my problem because i need a manual connection to the database
but thanks for the fast reply.
anyone can help me?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.