Codeigniter2 executing query on wrong database.. bug? - php

I know CodeIgniter 2 pretty well and now I have a weird bug with an existing project and I can't figure it out.
$this->db->flush_cache(); // Just to be sure
echo $this->db->database; // Displays "database_A"
$query = $this->db->get('table_X');
Then I get the error:
A Database Error Occurred
Error Number: 1146
Table 'database_B.table_X' doesn't exist
SELECT * FROM (`table_X`) WHERE `account_name` = 'bob'
Filename: /home/naturbl9/public_html/app/models/my_model.php
Line Number: 132
You can clearly see that echoing the currently used database displays "database_A", yet CodeIgniter is trying to execute a query in "database_B" that is used somewhere else (and used with a different name on top of that! Not "db"). If I set "pconnect" to false, the issue doesn't occur however..
database.php config:
(database_B is loaded by using the same config but changing $db['default']['database'] dynamically to "database_B"):
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database_A';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE; // If set to false, the bug doesn't occur!
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Related

Unable to select the specified database: conection to localhost (PHP with MySQL) in windows 10

I've been trying to connect to my database so I can make some changes in a website, in a local way, not directly on the server. But I always come to this error:
"Unable to select the specified database: backup"
. It says that the problem is in the file:
Filename: C:\xampp\htdocs\system\database\DB_driver.php
Line Number: 141.
But this is what I have in the line:
$this->display_error('db_unable_to_select', $this->database);
My script is like this:
$db['default']['username'] = 'teste';
$db['default']['password'] = '123456';
$db['default']['hostname'] = 'localhost';
$db['default']['database'] = 'backup';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = 'tbl_';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
I changed the name of the driver, but it didn't work as well.
the line of code that try to select the db:
function db_select()
{
return #mysqli_select_db($this->database, $this->conn_id);
}
How to proceed?

code igniter error: Unable to connect to your database server using the provided settings. Filename: core/Loader.php Line Number: 346

I'm developing a website application using codeigniter at localhost and then i host it in hawkhost.
At localhost it runs well, but at host there's and error :
Unable to connect to your database server using the provided settings.
Filename: core/Loader.php Line Number: 346
this is my db configuration
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '****';
$db['default']['password'] = '****';
$db['default']['database'] = '****';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
I know many people already asked this problem but it doesn't seems to solve anything on my case.
changing $db['default']['db_debug'] = TRUE; to false only hide the error but when i tried to get data from query it doesn't return anything.
I also tried to change $db['default']['pconnect'] = FALSE; to true and still doesn't work.
Any help please?
Just change localhost to the local ipaddress i.e. 127.0.0.1 and it will start working.
$db['default']['hostname'] = 'localhost';
to
$db['default']['hostname'] = '127.0.0.1';
In my case setting pconnect to false worked.
$db['default']['pconnect'] = FALSE;
The problem is really due to credentials.
I test it using simple code to check if connection can be made.
The problem is in username, i realize there's additional suffix when using hosting. such as suffix_user
I think the same errors in Core/Loader.php Line Number: 346 has the same solution. make sure the username, password and host are right.
It's solved. Thank you everyone for helping.
Just change
$db['default']['hostname'] = 'localhost';
to
$db['default']['hostname'] = '127.0.0.1';
and voila.
Most of the time this problem happens when codeigniter is in live environment.
That’s why I assumed a domain name ‘(example: domainname.com)’ and after creating a mysql database (example: testDb) and user name (example: testUser) you will find a prefix name automatically added with your mysql database name and user name like ‘domainname_ testDb’ and ‘domainname_ testUser’.
Now in codeigniter /application/config/database.php edit following:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'domainname_ testDb ';
$db['default']['password'] = '*******';
$db['default']['database'] = 'domainname_ testUser';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre']= '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'domainname_ testDb ';
$db['default']['password'] = '*******';
$db['default']['database'] = 'domainname_ testUser';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre']= '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
If anyone else is having this issue, try disabling SE-Linux if you have it enabled. It was causing the connection issues for me, with php 5.4.
Also worth checking is switching
$db['default']['dbdriver'] = 'mysql';
to
$db['default']['dbdriver'] = 'mysqli';
as mysql is depreciated after php 5.5.

postgresql configuration gives a blank page in Codeigniter (fail connection)

I just created my PHP Application using CodeIgniter framework and PostgreSQL for the database, it run well on localhost. Then I moved the application into the server (xxx.xxx.1.77), but it gave a blank page when I try to run.
I removed all libraries and packages in autoload.php it run well. So I put it one by one and run it after I put each libraries/packages. Finally, I found the problem : Database.
This one will show welcome page (default), but when I call one of my controller that connect to a model, it will show a blank page.
$autoload['libraries'] = array('session', 'form_validation');
This one will show a blank page at the first (welcome/default page won't show).
$autoload['libraries'] = array('session', 'form_validation', 'database');
So, this is my database configuration:
$db['default']['hostname'] = '192.168.1.4';
$db['default']['port'] = '8432';
$db['default']['username'] = 'postgres';
$db['default']['password'] = '';
$db['default']['database'] = 'gstseminar';
$db['default']['dbdriver'] = 'postgre';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
That configuration has no problem when I try to run my application on localhost.
Anyone knows about my problem? Any answer is appreciated.
I don't know how and why my project run well when I try to run it today. I looked at the config/database.php, and I found the configuration such as:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '192.168.1.4';
$db['default']['port'] = '8432';
$db['default']['username'] = 'postgres';
$db['default']['password'] = '';
$db['default']['database'] = 'gstseminar';
$db['default']['dbdriver'] = 'postgre';
$db['default']['dbprefix'] = 'tb_';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
and on my config/autoload.php are:
$autoload['libraries'] = array('session', 'form_validation', 'database');
It runs well now. Thank you.
i think the port you use was wrong
try this :
$db['default']['port'] = '5432';
i think its the default port for postgre

mysql_connect(): Access denied for user 'mss'#'localhost'

I'm having this error
Severity: Warning --> mysql_connect(): Access denied for user 'mss'#'localhost'
I'm using codeIgniter and I'm configuring the database this way:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'cms';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = TRUE;
$this->load->database($config);
I don't get where he's getting the mss username. At first I had mss in this config file but I changed to root
The only possible answer to your question is that you haven't uploaded the file correctly.
Check using an SSH connection that the file really has changed. If not, then you have to check your editor/IDE to see if it isn't your configuration that is incorrect and you might be uploading the files in the wrong place.

CodeIgniter issue with database

I am following a beginner's tutorial for CodeIgniter, but I can't get the view to load correctly because I have a database connection error. I have followed everything step-by-step, and gone through it again several times, but not been able to fix it. Could someone please help me, or at least explain why this is happening?
Here is the database configuration I currently have setup:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'helloworld';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
I've also autoloaded the database library with the following code:
$autoload['libraries'] = array('database');
I would also like to point out that I am using PHP version 5.3.13. I not sure if the version of PHP would affect my code, so I added it anyway.
The exact error I'm getting is:
A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: C:\wamp\www\CI\system\database\DB_driver.php
Line Number: 124
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root'; // default username if has not been set in phpmyadmin
$db['default']['password'] = ''; //leave it blank if has not been set in phpmyadmin
$db['default']['database'] = 'helloworld'; // this is your database name
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
you might have tried to import the project to work space.Instead try to traverse to the project folder through project explorer

Categories