Welcome Guest, Not a member yet? Register   Sign In
Db connects with mysql_connect but not database autoload
#1

[eluser]CJL01[/eluser]
I'm struggling with a problem that should be really basic... I've got a fresh copy of CI 1.7, and I'm trying to simply connect to a MySQL database.

If, in my default controller I add;

Code:
$dbh=mysql_connect ("localhost", "my_user","my_password") or die('Cannot connect);
mysql_select_db ("my_database");

Then everything is fine - no errors. If, however, I put exactly the same config details in config/database.php, and then autoload the database helper, I get a database connection error.

Code:
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "my_user";
$db['default']['password'] = "my_password";
$db['default']['database'] = "my_database";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

I must be doing something really stupid because this has to be CI lesson 1...
#2

[eluser]Thorpe Obazee[/eluser]
[quote author="CJL01" date="1244014791"]
Then everything is fine - no errors. If, however, I put exactly the same config details in config/database.php, and then autoload the database helper, I get a database connection error.[/quote]

you should load the database library
#3

[eluser]CJL01[/eluser]
Apologies - that was a typo...

I'm autoloading the database library (not a database helper - which doesn't exist!).
#4

[eluser]Colin Williams[/eluser]
Get into the DB class and debug.
#5

[eluser]CJL01[/eluser]
After a bit of investigative work I've found that the error is being generated in the db_driver class when the connection is made to the database (actually this isn't very surprising at all!!)

I'm still unsure exactly what to do about correcting it though... Any suggestions?
#6

[eluser]picardo[/eluser]
I'm getting the exact same error. Did anyone solve this problem yet?

database library is being autoloaded, but the db object cannot be found.
#7

[eluser]picardo[/eluser]
I figured out what the problem was. It turns out that I had a second $config['libraries'] instance at another part of the config.php file. I used it to load a single library, not seeing the first. But that was overwriting the first one, where the database library was being loaded. When I removed the second line, the problem went away by itself.
#8

[eluser]CJL01[/eluser]
I did actually solve this problem, although it is probably to do with my hosting provider than CI.

My website is hosted with Eukhost, and I'm on a shared server, so it turns out that there was a db_prefix that was related to my account with Eukhost that I needed to specify in my CI config to get it to connect. Also, I changed the driver from mysql to mysqli. For some reason I didn't need either of these things with the standard PHP mysql_connect but I did need it for CI. Now it connects fine.

Good luck!




Theme © iAndrew 2016 - Forum software by © MyBB