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

[eluser]wilfred[/eluser]
I have a simple application up and running. But I cant connect to the database using the following code where my table name is called hippo.

<?php
class Blog extends Controller{

function Index()
{
$data['title'] = "party zone";
$data[heading] = "lets rock and keep it rocking";

$this->load->view('blogview',$data);

$this->load->database();

$sql = "SELECT * FROM hippo WHERE manufacturer = ?";

$this->db->query($sql, array('EABL'));
}

}
#2

[eluser]James Bolongan[/eluser]
Hello Welfred,

Be sure you have configured the database.php in the config folder.
#3

[eluser]khagendra[/eluser]
It is tedious to load database connection in every function. U can load globally that will connect to database and will be easy to use.

1. open database.php file in config\database.php and set some variables
Code:
$db['default']['hostname'] = "";  
$db['default']['username'] = "";  
$db['default']['password'] = "";        
$db['default']['database'] = "";

2. Now set the line as below in config\autoload.php
Code:
$autoload['libraries'] = array('database');

Now, the database settings in config\database.php will be loaded globally.
#4

[eluser]Unknown[/eluser]
hi... i am facing the same problem, when i changed as you(khagendra) said above..
$autoload['libraries'] = array('database'); but i am getting error like

A Database Error Occurred

Unable to connect to your database server using the provided settings.
#5

[eluser]CI_avatar[/eluser]
@kiranmay: you may not change the autoloads to you database class. just initialize it before you use it.

how to initialize: $this->load->database();

after you initialize you may now use the db functions.
#6

[eluser]CI_avatar[/eluser]
@wilfred: please check your $this->db->query($sql, array('EABL'));
when you are going to use db->query the argument their is only the query string.
Like this:

$this->load->database();
$sql = “SELECT * FROM hippo WHERE manufacturer = ?”;
$this->db->query($sql);

see the documentation for more info.




Theme © iAndrew 2016 - Forum software by © MyBB