Setting up cleardb for codeigniter on heroiku - codeigniter

I am using cleardb on heroku to setup a codeigniter application but I have been getting errors even though the values I have inserted were all correct. I am providing all the details of the configuration below as this is just a test app. I am also posting a screen shot of the errors, any tips would be appreciated. Please do not tell me about how i should use mysqli or PDO instead of this as I have looked at the support and it doesn't exist yet.
$active_group = 'default';
$active_record = TRUE;
$url=parse_url(getenv("mysql://be581f53f6c3cc:e3dffb73#us-cdbr-east-05.cleardb.net/heroku_29745033f6a8f32?reconnect=true"));
$db['default']['hostname'] = 'us-cdbr-east-05.cleardb.net';
$db['default']['username'] = $url["be581f53f6c3cc"];
$db['default']['password'] = $url["e3dffb73"];
$db['default']['database'] = substr($url["heroku_29745033f6a8f32"],1);
$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;
Here are the errors

Don't know if this is still active, but you should install composer, enable mysql in composer.json like this:
{
"require": {
"ext-mysql": "*"
}
}
and then run
composer install
For more information you can check this

I created connection in a simple way
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'us-cdbr-iron-east-01.cleardb.net';
$db['default']['username'] = 'b20a.....';
$db['default']['password'] = '26e9...';
$db['default']['database'] = 'heroku_5....';
$db['default']['dbdriver'] = 'mysql';
and just update composer.json as
{
"require": {
"ext-mysql": "*",
"monolog/monolog": "1.2.*"
}
}
and then run
composer install
then
git add .
git commit -m
git push .............
and it's working fine.

Related

How connect codeigniter 2 to informix via DSN

Hello i have a dsn via odbc connection in my machine. When connect with pure php it´s work.
$_dsn= "informix:DSN=araucaria";
$pdo = new PDO($this->_dsn);
$pdo->setAttribute(3, 2);
How connect codeigniter 2 vía dsn ? My Database CI configuration like this
$db['default']['hostname'] = 'informix:DSN=araucaria';
$db['default']['username'] = 'xxxx';
$db['default']['password'] = 'xxxx';
$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;
Help me please!

readreplica implementation with codigniter

I'm developing PHP application with codigniter framework. And using amazon read replica setup. I need a way to handle all select query through read only DB and all update related query through master DB. Following is the current usage I use in my script.
database config file
$db['default']['hostname'] = 'master_db_host';
$db['default']['username'] = 'dbuser';
$db['default']['password'] = 'dbpassword';
$db['default']['database'] = 'db_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;
model script
class replica_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function write($data)
{
$this->db->insert('dumy', $data);
return $this->db->insert_id();
}
public function read()
{
$query = $this->db->get("dumy");
return $query->result();
}
}
Can any one help me to alter the script to access master and replica based on read and write?
We can handle multiple DB with codeigniter. So first we can see how the config file need to be set.
$active_group = 'default';
$active_record = TRUE;
//Master DB config values
$db['default']['hostname'] = 'master_db_host';
$db['default']['username'] = 'db_user';
$db['default']['password'] = 'db_pass';
$db['default']['database'] = 'db_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;
//Replica DB config values
$db['read_replica']['hostname'] = 'replica_db_host';
$db['read_replica']['username'] = 'db_user';
$db['read_replica']['password'] = 'db_password';
$db['read_replica']['database'] = 'db_name';
$db['read_replica']['dbdriver'] = 'mysql';
$db['read_replica']['dbprefix'] = '';
$db['read_replica']['pconnect'] = FALSE;
$db['read_replica']['db_debug'] = TRUE;
$db['read_replica']['cache_on'] = FALSE;
$db['read_replica']['cachedir'] = '';
$db['read_replica']['char_set'] = 'utf8';
$db['read_replica']['dbcollat'] = 'utf8_general_ci';
$db['read_replica']['swap_pre'] = '';
$db['read_replica']['autoinit'] = TRUE;
$db['read_replica']['stricton'] = FALSE;
Parameter "$db['read_replica']['pconnect']" should be true on master db config and it should be false on replica config. Other wise when we enable replica db master db config also overwritten by replica config.
Then model usage will be like follows.
class Replica_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->db_replica = $this->load->database('read_replica',TRUE);
}
public function write($data)
{
$this->db->insert('dumy', $data);
return $this->db->insert_id();
}
public function read()
{
$query = $this->db_replica->get("dumy");
return $query->result();
}
}
CI allows you to connect to multiple databases via groups.
This is directly from their site:
https://ellislab.com/codeigniter/user-guide/database/connecting.html
$DB1 = $this->load->database('read', TRUE);
$DB2 = $this->load->database('write', TRUE);
hen you connect this way, you will use your object name to issue commands rather than the syntax used throughout this guide. In other words, rather than issuing commands with:
$this->db->query();
$this->db->result();
etc...
You will instead use:
$DB1->query();
$DB1->result();
etc...
In you will have a multi-dimensional array for your db
$db['read']['hostname'] = ...
$db['write']['hostname'] = ...
So when you read, you would use $DB1, when you write, you'd use $DB2 ...
I haven't used this exact setup. But that's the gist of it.

Configuring config/database.php file using php script

I have a script in my controller which is as follows:
if($this->db->query("CREATE DATABASE IF NOT EXISTS db_ecommerce"))
{
$sql=file_get_contents('./databse_backup/backup.sql');
foreach (explode(";\n", $sql) as $sql)
{
$sql = trim($sql);
//echo $sql.'<br/>============<br/>';
if($sql)
{
$this->db->query($sql);
}
}
}
It creates a database and then runs a backup sql file.
My issue is that we need to configure the config/database.php file beforehand.
First I want this script to run and after that the database.php file should be changed.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'db_name';
Since its now for localhost hence,
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db_name';
An approach would be to have 'template file' in your installation folder like:
ex. database.php
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '<?php echo $hostname;?>';
$db['default']['username'] = '<?php echo $username;?>';
$db['default']['password'] = '<?php echo $password;?>';
$db['default']['database'] = '<?php echo $database;?>';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '<?php echo $prefix;?>';
$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 then you can get the values for db connection (from a form for example) and write the new file in your codeigniter application (application/config/database.php)
ex.
$settings['hostname'] = $this->input->post('hostname');
$settings['username'] = $this->input->post('username');
$settings['password'] = $this->input->post('password');
$settings['database'] = $this->input->post('database');
$settings['prefix'] = $this->input->post('prefix');
$file_contents = $this->load->view('templates/database', $settings, true);
write_file($_SERVER['DOCUMENT_ROOT'].'application/config/database.php', $file_contents);

Not able to access remote database

I am trying to use a remote database for my codeigniter project (I am using WAMP).I get the following error when I try to bring up my controller page:
A Database Error Occurred Unable to connect to your database server using the provided settings. Filename:
C:\wamp\www\Naranpur\system\database\DB_driver.php Line Number: 124
These are my database file settings:
$db['default']['hostname'] = 'something.cs.something.edu';
$db['default']['port'] = '3306';
$db['default']['username'] = 'some_user';
$db['default']['password'] = 'some_pwd';
$db['default']['database'] = 'some_database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = TRUE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'latin1';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
But when I try to access the database on my localhost with the following settings and give the same url, it works!
$db['default']['hostname'] = 'localhost';
$db['default']['port'] = '3306';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = TRUE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'latin1';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Could someone please tell me where I might be going wrong?
I think that you need to give the user some_user the privilege to access your database.
from your phpmyadmin got to privilege tab and then edit the privilege for this user.
In the login information section set the host to your IP address or set it to % to allow this user to access from any host.
Can you access to something.cs.something.edu from a mysql client? (to check the connection)

CodeIgniter MultiDatabse connect error

I see a lot of method/tutorial to connect multi database in CodeIgniter? Still far far away with me. Some one provide me to connect multi database in CI. This is my using way i was found it in a blog. pardon me, i don't recognize the blog address.
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'vk_global';
$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;
//add alternate database settings
$db['wow']['hostname'] = 'localhost';
$db['wow']['username'] = 'root';
$db['wow']['password'] = 'root';
$db['wow']['database'] = 'auth';
$db['wow']['dbdriver'] = 'mysql';
$db['wow']['dbprefix'] = '';
$db['wow']['pconnect'] = TRUE;
$db['wow']['db_debug'] = TRUE;
$db['wow']['cache_on'] = FALSE;
$db['wow']['cachedir'] = '';
$db['wow']['char_set'] = 'utf8';
$db['wow']['dbcollat'] = 'utf8_general_ci';
$db['wow']['swap_pre'] = '';
$db['wow']['autoinit'] = TRUE;
$db['wow']['stricton'] = FALSE;
i was call it with this code
$DB2 = $this->load->database('auth',true);
and i was insert a simple row with this code
$this->$DB2->insert('events_grouptable',$data);
i got this error You have specified an invalid database connection group.
how should i solve this error?
$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('wow', TRUE);
You need to set which db to use with which group otherwise the alternative does not work as
$active_group = 'default';
More details are here http://ellislab.com/codeigniter/user-guide/database/connecting.html

Resources