I am trying to read a existing sqlite database file in codeigniter using PDO connection. My question is actually two fold:
Reading the existing sqlite database actually creates a new db file along with the original one. assume the original db filename is a.db, the new one will be a.db;dbname=.
Subsequent db statements operate on the a.db;dbname=. I have tried to run create table, insert new value on this new db file. both run fine. However, select statement returns nothing and no error either.
I am baffled at this point.. Please help.
updated with the related settings:
// sql
$this->db->query('create table x (y int)');
$this->db->query('insert into x values(6)');
$result = $this->db->get('x')->result(); // $result is an empty array
// config/database.php
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'sqlite:/home/perlwle/db/a.db';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'pdo';
$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;
/home/perlwle/db/a.db is the sqlite database file and owned and writable by www-data.
thanks.
This is a bug in CodeIgniter; you have to update.
Related
I am working on project with codeigniter, all working properly only during one table operations, its showing error "table does not exist". Actually that table is present in database. Everything working properly on localhost. Only causing error "table does not exist" for only one table. Otherwise remaning things in project are working properly on live also.
I tried with following way:
$db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE;
are changed to FALSE.. then its showing "page is not responding" issue.
My Code Snippet (Of Model File):
public function getClubmember($edit_id = 0)
{
$this->db->select(" * ");
$this->db->from('XXXXXXX as XX');
if($edit_id){
$this->db->where('XX.id',$edit_id);
}
$this->db->order_by('XX.id', 'desc');
$result = $this->db->get();
return $result->result_array();
}
My Config Code:
....
....
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'XXXXXXX';
$db['default']['password'] = 'XXXXXXX';
$db['default']['database'] = 'XXXXXXX';
$db['default']['dbdriver'] = 'mysqli';
$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;
....
Please suggest me the changes to resolve issue of "table does not exists". Only issue one table.. remaining project working fine.
I also faced such issue, you just need to check table name. It should be as it is. If one of the character may be capital in table of script. At such situation, task work properly on local, but give such error on live.
Just check script table name spell. Is there any letter capital or small because of which not matching with db table name.
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;
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
i am getting the following error :
Unable to connect to your database server using the provided settings.
Filename: core/Loader.php
Line Number: 346
my database.php is :
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'usertest';
$db['default']['password'] = '';
$db['default']['database'] = 'usertest';
$db['default']['port'] = '5433';
$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;
What is the possible solution. I do not want to $db['default']['db_debug'] = FALSE;
as even after doing this i am still not able to connect to db.
EDIT :
Changed the port to 5432. still it doesnt work
Set Set $db['default']['pconnect'] = FALSE; and check once again.
Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might
require a full DSN string to be provided. If that is the case, you
should use the ‘dsn’ configuration setting, as if you’re using the
driver’s underlying native PHP extension, like this:
$db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';
from official documentation of codeigniter
Try:
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
Check your db driver name. I am pretty sure it is "postgres" and not "postgre."
I want to change some database settings for one part of my program.
In my setup the databse class is autoloaded with a config which looks like this
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = '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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
At one part of the script I want to change the value of $db['default']['keyname'] , how can I do this?
You should be adding another set of credentials instead of changing the existing ones dynamically:
$db['another_db']['hostname'] = 'localhost';
$db['another_db']['username'] = 'root';
$db['another_db']['password'] = '****';
$db['another_db']['database'] = 'database';
$db['another_db']['dbdriver'] = 'mysql';
$db['another_db']['dbprefix'] = '';
$db['another_db']['pconnect'] = TRUE;
$db['another_db']['db_debug'] = TRUE;
$db['another_db']['cache_on'] = FALSE;
$db['another_db']['cachedir'] = '';
$db['another_db']['char_set'] = 'utf8';
$db['another_db']['dbcollat'] = 'utf8_general_ci';
$db['another_db']['swap_pre'] = '';
$db['another_db']['autoinit'] = TRUE;
$db['another_db']['stricton'] = FALSE;
You can load this other database by doing:
$another_db = $this->load->database('another_db', TRUE);
Then use it like normal database driver:
$another_db->select();
...etc
There is how class > check it http://codeigniter.com/user_guide/libraries/config.html
How ever you can NOT change db connection settings.... They are loaded long before your configs..
Slightly hacky, but you could add something like this to system/database/DB_driver.php (I've used db password change as an example):-
public function update_pw($value) {
$this->password = $value;
}
Then in your project, do
$this->db->update_pw('example');
$this->db->reconnect();
Depending on specifically what you want to change in the config - and also more importantly, why you want to change it - this may or may not be the best option.
Also you can always use a Global var. Normally I always have 2 databases in place. One for production and one for development. You just check that var to load one or the other.
That concept can be used to load one database in one part of your application and other in other part. However, I would go with the built in solution like Brendan pointed out.