Welcome Guest, Not a member yet? Register   Sign In
how to connect to db server manually
#1

[eluser]4ever[/eluser]
How to connect to db server if I don"t want to set config/database.php
Code:
$db['default']['database'] = '';
but I want to connect to database in my controller site.php ?

I don't want any server/database connection start before I say.
#2

[eluser]InsiteFX[/eluser]
Then you need to do the old way with PHP.

InsiteFX
#3

[eluser]4ever[/eluser]
Well, I try but $db is not accessed in my controller:

Code:
function info(){
   $mysql_link = mysql_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password']);
   if (!$mysql_link) die('Could not connect Mysql server: ' . mysql_error());
  
   // $this->load->database("database");
   //$this->load->library("database");
   $this->load->model("db_load");  
   $this->db_load->generate_db();
   $this->load->view("info_view");  
  }

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: db

Filename: controllers/site.php

Where are saved data from $db? They have to be in some object right? How can I access the object with database configuration?
#4

[eluser]InsiteFX[/eluser]
You could also use DSN
Code:
$dsn = 'dbdriver://username:password@hostname/database';

$this->load->database($dsn);

This is all in the CodeIgniter User Guide.

InsiteFX
#5

[eluser]4ever[/eluser]
Fine, but I need just to connect the server before I check that db exists. If not then I create a database. So the db needn't exist. I still look for the db configuration object
#6

[eluser]4ever[/eluser]
Solved!

Modul name: db_load.php
Creates database and table if one of these two does not exist.

Code:
<?php

class Db_load extends CI_Model {

/*
  $dsn = 'dbdriver://username:password@hostname/database';
  $this->load->database($dsn);
*/

  function table_exists($table, $db) {
      $tables = mysql_list_tables ($db);
      while (list ($temp) = mysql_fetch_array ($tables)) if ($temp == $table) return TRUE;
      return FALSE;
  }

function generate_db(){
  $database = "my_db";
  $table_name = "table_name";
  $this->load->database();
  $CI =& get_instance();
  $db =& $CI->db;
  $mysql_link = mysql_connect( $db->hostname, $db->username, $db->password);

  // $this->load->dbforge(); // DEPENDENCE: CI_DB_utility extends CI_DB_forge
  // $this->load->dbutil(); // INIT: Database Utility Class
                              
  if ( !mysql_select_db($database) ):
       $this->load->dbforge();    // LOAD
       $this->load->dbutil();
       $db_util_loaded = true;
     if (!$this->dbforge->create_database('my_db')) die('Database not created!');
  endif;

  if ( !$this->table_exists($table_name, $database) ):
      if ( !isset($db_util_loaded)): // LOAD
       $this->load->dbforge();
       $this->load->dbutil();
      endif;
      if ( !mysql_select_db($database) ) die("fail");
  // DECLARING VALUES
  $fields = array(
    'users' => array(
      'type' => 'VARCHAR',
      'constraint' => '100',    
      ),
    'id' => array(
      'type' => 'INT',
      'constraint' => 5,
      'unsigned' => TRUE,
      'auto_increment' => TRUE
      ),
    'title' => array(
      'type' => 'VARCHAR',
      'constraint' => '100',
      ),
    'author' => array(
      'type' =>'VARCHAR',
      'constraint' => '100',
      'default' => 'King of Town',
      ),
    'desc' => array(
      'type' => 'TEXT',
      'null' => TRUE,                
      ),
    );
    $this->dbforge->add_field($fields);

    // ADD PRIMARY KEY
    $this->dbforge->add_key('id', TRUE);        
    // NEXT WAYS
    // $this->dbforge->add_key('blog_name'); // KEY    
    // $this->dbforge->add_key(array('blog_name', 'blog_label')); // KEY

    // CREATING TABLE
    $this->db->query('use ' . $database );
    if ( !$this->dbforge->create_table($table_name)) die("Table not created!");

  endif;
}

  function get_all(){
   // $q = $this->db->get("");
  }

}

?>

Controller site Method:

Code:
function info(){
   $this->load->model("db_load");  
   $this->db_load->generate_db();
   $this->load->view("info_view");  
  }

Thanks to InsiteFX's help.

If you could check my code please... I hope it is all ok. Database and Table created to me.
#7

[eluser]ddutra[/eluser]
Great class, gratz for those that made it.

I made a small modification to get the desired behavior.

I made all errors, warnings, notices, everything, halt the application, even in production. The errors and not shown on the user screen (class default behavior), instead the errors are logged as .html files and mailed to the admiin. The user will see a simple /error page.

The .html file the admin receives by email contains everything needed to re-produce the error and debug it.


Best regards.




Theme © iAndrew 2016 - Forum software by © MyBB