Problem with db connection (beginner) |
[eluser]4ever[/eluser]
Hi I am beginner with CI. I start with two video tutorials, but I am not confident with the solution they use. They create db and table manually in phpmyadmin. I want to do it in my module so I created class Db_load and method generate_db() in it. I want to call this method from page site.php . Because I want to define my database name in the site.php and then th generate db and table. I leave $db['default']['database'] = ''; empty. However when I try to run my code it says I'm not connected to server. Code: <?php Code: class Db_load extends CI_Model { Result: A PHP Error was encountered Severity: Notice Message: Undefined property: Site::$db Filename: core/Model.php Line Number: 50 I didn't find solution on internet so I hope you can help me. And config of sure: Code: $db['default']['hostname'] = 'localhost'; No db name set ... I expect I want to set it in my model.
[eluser]4ever[/eluser]
[quote author="InsiteFX" date="1305468014"]CodeIgniter User Guide DBForge InsiteFX[/quote] That's OK. The class continues with this line Code: $this->load->dbforge(); I've just shorten the code. I will also use dbutil to check if db exists :-) but now looking for the object with database configuration. Can you tell me where I can find list of the most important objects? For now I know about $this but $this doesn't contain db information.
[eluser]4ever[/eluser]
I found DB_utility.php which contains a constructor: Code: function CI_DB_utility() So with get_instance(); I could get the db object I need. however this constructor should be called when the class is initiated: Code: $this->load->dbutil();
[eluser]InsiteFX[/eluser]
It looks like you did not load the database! Did you load the database? application/config/autoload.php Add the database to the libraries section. InsiteFX
[eluser]4ever[/eluser]
You're right. I commented out the line because I will not use database on every page... So with autoload database: A PHP Error was encountered Severity: Notice Message: Undefined variable: CI Filename: models/db_load.php // that is the print_r($CI); Line Number: 10 So I thought I could to call it in my module with no autoload config: Code: function generate_db(){ But this gives: An Error Was Encountered Unable to load the requested class: database
[eluser]4ever[/eluser]
Problem solved! Thanx to InfisiteFX's Help See the code here to create database and table if it doesn't exist: http://ellislab.com/forums/viewthread/189101/ |
Welcome Guest, Not a member yet? Register Sign In |