Welcome Guest, Not a member yet? Register   Sign In
Trouble loading database library
#1

[eluser]chiggles[/eluser]
I'm working through the video tutorials, and I've hit a snag when attempting to load the database library and connect to a database.

I found this thread which gets very close to the issue:
http://ellislab.com/forums/viewthread/60534/


My problem is that the recommended line in autoload.php...
$autoload['libraries'] = array('database');

...causes the application to return a status code of 200 with no data.


I tried using the older style marked in the comments of autoload.php as deprecated...
$autoload['core'] = array('database');

...and the result was an error when attempting to use the $db variable in my controller. I suspect that this is because the deprecated version had no effect whatsoever.

I'm using codeigniter version 1.5.4

Any help is hugely appreciated!
#2

[eluser]xwero[/eluser]
have you set up the config database file?

If the problem still exists remove the autoload and add to your controller
Code:
$this->load->databes();
#3

[eluser]chiggles[/eluser]
My config database file contains the following:

$active_group = "default";

$db['default']['hostname'] = "myhostname";
$db['default']['username'] = "myusername";
$db['default']['password'] = "mypassword";
$db['default']['database'] = "mydatabase";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['active_r'] = TRUE;
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";

Also, I've been picking through it and so far I've traced the failure to line 81 of DB.php

Here's the code, along with echo statements I've added for debugging:

// Instantiate the DB adapter
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';
echo "driver is $driver, params are:<br />";
foreach($params as $param) {
echo "$param <br />";
}
$DB =& new $driver($params);
echo "Test code! <br />";
return $DB;
}

This code returns the following html (with line breaks following each <br /> for clarity):

driver is CI_DB_mysql_driver, params are:<br />
myhostname <br />
myusername <br />
mypassword <br />
mydatabase <br />
mysql <br />
<br />
1 <br />
1 <br />
1 <br />
<br />
<br />


The last echo statement ("Test code!") is never reached.
#4

[eluser]xwero[/eluser]
check your database/driver directory and check if you got the CI_DB_mysql_driver.php file. If not you have to download CI again.
#5

[eluser]chiggles[/eluser]
My system/database/drivers/mysql directory contains:

index.html
mysql_driver.php
mysql_result.php
mysql_utility.php

I assume this is as it should be, since I just downloaded a fresh CodeIgniter zipfile which contained the same. I am not finding a file called CI_DB_mysql_driver.php anywhere. I don't know enough about the code to understand the intent of that CI_DB_ prefix. Is it supposed to be there? Is there supposed to be a file by that name?

p.s.

I tried adding:
$this->load->database();

(I assume that's what you meant)

to the constructor of my controller, and then in the function where I attempt to access $this->db with the same result
#6

[eluser]xwero[/eluser]
Ah no, i was a bit too focused on the code i guess.

And you are sure the connection data is correct? If that is the case i can't of anything else that would cause that error, i'm sorry.
#7

[eluser]chiggles[/eluser]
Well I am able to connect to the database from my desktop application...

If the connection were to fail due to incorrect connection data, would this be the behavior? A 200 Status and no data returned?



On closer examination, CI_DB_mysql_driver is the name of the class inside mysql_driver.php so mystery solved there:

class CI_DB_mysql_driver extends CI_DB {

On the other hand, I can't find a constructor for this class. I also can't find the CI_DB class it should be inheriting from.
#8

[eluser]xwero[/eluser]
The database library is complex because of the different drivers. It the CI_DB class gets build using the database config file and it gets passed by reference.

No it's not normal behaviour to get a 200 error, but i can't think of anything that could cause that error.
#9

[eluser]chiggles[/eluser]
[quote author="chiggles" date="1195140969"]On the other hand, I can't find a constructor for this class. I also can't find the CI_DB class it should be inheriting from.[/quote]

Nevermind, looks like that class is created dynamically by the code in DB.php immediately above where it's failing on me:

require_once(BASEPATH.'database/DB_driver'.EXT);

if ( ! isset($params['active_r']) OR $params['active_r'] == TRUE)
{
require_once(BASEPATH.'database/DB_active_rec'.EXT);

if ( ! class_exists('CI_DB'))
{
echo "Test code 1! class_exists('CI_DB') returns " . class_exists('CI_DB') . "<br />";
eval('class CI_DB extends CI_DB_active_record { }');
echo "Test code 2! class_exists('CI_DB') returns " . class_exists('CI_DB') . "<br />";
}
}
else
{
if ( ! class_exists('CI_DB'))
{
eval('class CI_DB extends CI_DB_driver { }');
}
}

require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);

// Instantiate the DB adapter
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';
$DB =& new $driver($params);
echo "Test code 3! <br />";
return $DB;


That code returns:

Test code 1! class_exists('CI_DB') returns <br />
Test code 2! class_exists('CI_DB') returns 1<br />


So, it seems to be creating CI_DB as a subclass of CI_DB_active_record, and then attempting to instantiate CI_DB_mysql_driver as a subclass of CI_DB?
#10

[eluser]chiggles[/eluser]
Ok, with that new revelation I've traced the failure farther, to line 142 of DB_driver.php:

echo "Test code 1! this->pconnect is $this->pconnect <br />";

// Connect to the database
$this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();

echo "Test code 2! <br />";

// No connection? Throw an error
if ( ! $this->conn_id)
{
log_message('error', 'Unable to connect to the database');

if ($this->db_debug)
{
$this->display_error('db_unable_to_connect');
}
return FALSE;
}

echo "Test code 3! <br />";



The above code is returning:

Test code 1! this_>pconnect is 1 <br />


The second test statement is never reached, and thus the "throw an error" section is not being reached either. It's just failing when attempting to connect, for some reason...

Any ideas? I'm just starting to learn php here...




Theme © iAndrew 2016 - Forum software by © MyBB