Welcome Guest, Not a member yet? Register   Sign In
Connecting to a database
#1

[eluser]grejsimojs[/eluser]
Hey peeps,

I'd like to connect my form to my database.
I've been looking through the user guide and it has gotten me a little confused, yet I think I understand it better now than before.

As I understand, I have to connect to the database using a model?
What should I really have in the model?

Where should this be written? In the model, in my controller or in database.php?
Code:
$this->load->database('');

Thank you!
#2

[eluser]InsiteFX[/eluser]
If you are using the database all the time you can autoload it in ./application/config/autoload.php as a library.

Otherwise load it in your constructor.
#3

[eluser]grejsimojs[/eluser]
Alright, I do not use it all the time.

Loading it in the constructor would be great, but how exactly do I do that? Is my constructor in the model?
#4

[eluser]Adam Liszkai[/eluser]
Hi!

You must set the database settings in your /application/config/database.php

And then load in your class or modell constuct:
Code:
function __constuct()
{
  parent::__construct();
  $this->load->database();
}

Or load somwhere (before you use database) in your php code:
Code:
$this->load->database();

And do not try load in view or template files Smile

And then read the documentation for more:
http://ellislab.com/codeigniter/user-gui...index.html

Cheers!
#5

[eluser]grejsimojs[/eluser]
Hey Adam! Thanks for the help, very appreciated!

This is what my model looks like right now:

Code:
<?
class Kanbanmodel01 extends CI_Model {

function__construct()
{
parent::__construct();
$this->load->database(kanban);
}
}

?>

How do I put the Model into work?
#6

[eluser]Adam Liszkai[/eluser]
If you have multiple databases then you must fill out the
/application/config/database.php

Code:
$config['hostname'] = "localhost";
$config['username'] = " YOUR USERNAME FOR DATABASES ";
$config['password'] = " YOUR PASSWORD FOR DATABASES ";
$config['database'] = " YOUR DEFAULT DATABASE NAME ";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = ""; // if you have prefix put it there

Then to access multiple database:

Code:
<?
/**
* @name: Kanban model
* @author: 'Adam Liszkai' <[email protected]>
* @return: Kanban model echo message :D
*/
class Kanban_model extends CI_Model
{
  public function __construct()
  {
    parent::__construct();

    // Load html helper
    $this->load->helper('html');

   /*
    *  Connect to the 'kanban' database, if is not the
    *  default in the /application/config/database.php
    *
    */

    $kanban_db = $this->load->database('kanban', TRUE);
  }

  /*
   * @name: Index function
   * @author: 'Adam Liszkai' <[email protected]>
   * @return: Kanban model echo message :D  
   */
  public function index()
  {
    echo(heading('KANBAAAAAAN! :D', 3)."\n\r");

    $query = $kanban_db->get('kanban_table');
    foreach ($query->result() as $row)
    {
       echo($row->kanban_cell.br(1)."\n\r");
    }

    // @TODO: Some more code! :D
  }
}
?&gt;

But if you have just one database, then i suggest fill out the database informations in /application/config/database.php

Code:
$config['hostname'] = "localhost";
$config['username'] = " YOUR USERNAME FOR DATABASE ";
$config['password'] = " YOUR PASSWORD FOR DATABASE ";
$config['database'] = " YOUR DATABASE NAME ";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = ""; // if you have prefix put it there

then use

Code:
&lt;?
/**
* @name: Kanban model
* @author: 'Adam Liszkai' <[email protected]>
* @return: Kanban model echo message :D
*/
class Kanban_model extends CI_Model
{
  public function __construct()
  {
    parent::__construct();

    // Load html helper
    $this->load->helper('html');

    // Load the database
    $this->load->database();
  }
  
  /*
   * @name: Index function
   * @author: 'Adam Liszkai' <[email protected]>
   * @return: Kanban model echo message :D
   */
  public function index()
  {
    echo(heading('KANBAAAAAAN! :D', 3)."\n\r");  

    $query = $this->db->get('kanban_table');
    foreach ($query->result() as $row)
    {
       echo($row->kanban_cell.br(1)."\n\r");
    }

    // @TODO: Some more code! :D
  }
}
?&gt;

this modell Smile

More examples: http://ellislab.com/codeigniter/user-gui...index.html
#7

[eluser]grejsimojs[/eluser]
Wow thanks for taking your time to do that one! Very appreciated!

I don't exactly understand everything in the code, such as the "\n\r". Also, do I have to load this model through the controller to make it work?
I don't quite understand fully what just this model does.

By the way, would you like to take this via mail instead?
#8

[eluser]grejsimojs[/eluser]
You around? :/
#9

[eluser]Adam Liszkai[/eluser]
Big Grin Sorry, about that, I'm really gone.

\n new line in console and in the rendered source code
\r new line in OSX console and in the OSX rendered source code




Theme © iAndrew 2016 - Forum software by © MyBB