Welcome Guest, Not a member yet? Register   Sign In
Problem with db connection (beginner)
#1

[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
class Site extends CI_Controller {

  function info(){
   // $this->load->database("database"); // how to connect to server?
   //$this->load->library("database"); // how to connect to server?
   $this->load->model("db_load");
   $this->db_load->generate_db(); // create db and table if it does not exist yet
   $this->load->view("info_view");  
  }
  
}

?>

Code:
class Db_load extends CI_Model {

function generate_db(){
  $database = "my_db";
  $table_name = "table_name";
  if (
     //!$this->db->database_exists($database) &&     // HOW TO TEST DB?
     !$this->db->table_exists($table_name)):
    // CREATE DATABASE  
...

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';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = '';

No db name set ... I expect I want to set it in my model.
#2

[eluser]InsiteFX[/eluser]
CodeIgniter User Guide DBForge

InsiteFX
#3

[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.
#4

[eluser]4ever[/eluser]
I found

DB_utility.php which contains a constructor:

Code:
function CI_DB_utility()
    {
        // Assign the main database object to $this->db
        $CI =& get_instance();
        $this->db =& $CI->db;

        log_message('debug', "Database Utility Class Initialized");
    }


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();
print_r($CI); // HOWEVER no $CI object is defined... in my module
die();
#5

[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
#6

[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(){
  $database = "my_db";
  $table_name = "table_name";
  $this->load->database();  // init Database
  $this->load->dbutil(); // init Database Utility Class
    print_r($CI);
    die();

But this gives:

An Error Was Encountered
Unable to load the requested class: database
#7

[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/




Theme © iAndrew 2016 - Forum software by © MyBB