I have a multi-tenancy application that I am developing and I'm nearly there.
Currently I am using one database per customer - copying it each time and naming depending based on $_SERVER['http_host']...
A lot of the tables each database never change, and contain the same information.
I would like to have a main application database that contains this information, including a list of clients and what there database is called. This database would then be used to select the correct customers database...
Is this possible and can anyone point me in the direction of a suitable tutorial?
Thanks
OK, so you'll need different database configs for your "administrative" database and your "customer" databases.
Use the examples from this page to generate those separate configs:
http://ellislab.com/codeigniter/user_guide/database/configuration.html
Then, in your main controller (please tell me your extending a MY_Controller.php and not just using CI_Controller.php), you'll want to read your 'admin' database and get all client information. You'll then want to generate a $config array for this client database and reload the database with the new $config array:
// in MY_Controller constructor
// do your thing here to connect to the admin database and get client details
// ...
//
$config['hostname'] = "localhost";
$config['username'] = $client_db_username; // you got this from the 'admin' database
$config['password'] = $client_db_password; // you got this from the 'admin' database
$config['database'] = $client_db_name; // you got this from the 'admin' database
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
You'll then have a connection to the client database.
Note
If you'll need to connect to both databases back and forth, then you can connect to multiple databases as explained on this page: http://ellislab.com/codeigniter/user_guide/database/connecting.html
Related
I am facing the issue of localhost/joomla is not showing login page, just show word Error
I have attached permission details and other corresponding files. Please help me to solve this issue.
Thankyou for your time.
Seems like the database the site it's not connected.
Check your configuration.php (in the root of the site) for this parameters:
public $dbtype = 'mysqli'; // Driver you are using to connect to the MySQL database.
public $host = 'localhost'; // The host for the database. If it's in the same server, it's ok like this.
public $user = 'root'; // Username for the database
public $password = 'root'; // Password of the database
public $db = 'NAME_OF_THE_DATABASE'; // Name of the database.
i have start a new application html5 and i need to send from that app the info user_points to the database i have the code that send a fixed amount points.
But Now I need to change the php code to receive ajax request to update is points on database From My html5 app.
what i can change to this php code to receive the ajax variable user_points and store in database?
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
// Retrieve data from database
$result=mysql_query($sql);
//$sql = mysql_query("UPDATE `users` SET points=`points`+5 WHERE user_id='".$_SESSION['user']."'");
$user_id = mysql_real_escape_string($_SESSION['user']);
$sql = mysql_query("UPDATE `users` SET user_points = user_points +'user_points' WHERE user_id = " . $_SESSION['user']);
// close MySQL connection
mysql_close();
?>
assume your php script is called run.php
and assume run.php is called via ajax like this http://yourserver/run.php?user_points=10&userid=12&sessiontan=8162836zqwizß182402834080oio
your script run.php would need to look like this:
<?php
session_start();
include_once 'dbconnect.php';
if(isset($_GET['userid']) and isset($_GET['user_points'] and isset($_GET['sessionpin']))) {
$res=mysql_query("SELECT userid FROM sessiontans WHERE user_id=x".dechex($_GET['userid'])" and sessiontan=x".dechex($_GET['sessiontan']));
if($row = mysql_fetch_array($res)) {
$sql = mysql_query("UPDATE `users` SET user_points = user_points + x".dechex($_GET['user_points'])." WHERE user_id = x" . dechex($_GET['userid']));
}
}
// close MySQL connection
mysql_close();
?>
As far as I am aware the session context is not available in ajax called scripts. So security wise that needs to 'simulated' via sessiontans or similar mechanisms. I assumed now, you would follow a sessiontan approach. A session tan be created in your app as soon as the user logs and is via time stamps only valid for a tiny time frame (a couple of minutes/hours). A sessiontan would need to be different from session-IDs.
I personally would add more sophisticated checks on the user-points to make sure that the those are not tricked... Anyway that is more on the 'game logic' side and would require a lot more insight on what this is all about.
Also in terms of performance tuning both statements could become part of a multi query: http://php.net/manual/de/mysqli.multi-query.php
Again this is advanced programming tactics and superexceeds the scope of your question.
I have 3 login fields (name, pass, company id). Now when user clicks "Log me in" button CI should select and work with database according to company id (and check user in this database and when he is logged in, work with this database over whole session /each company has its own database/).
How can I do this?
I found this but it looks like it is not the right solution.
Many thanks
CodeIgniter: Connection to your Database
Maybe the link above will help you out.
You have to set a new db connection for the specific company. In CodeIgniter it is really simple.
Combine it with THIS PROBLEM to get access to the database settings setted in the database-config.
Example:
<?php
$config = array();
$config['hostname'] = $this->db->hostname;
$config['username'] = $this->db->username;
$config['password'] = $this->db->password;
$config['database'] = 'companydb';
$config['dbdriver'] = $this->db->dbdriver;
$companydb = $this->load->database($config);
$companydb->query('SELECT ... FROM ... WHERE ...');
?>
Is it possible to use different database for authenticating user through Ion Auth?
I would like to create a database dedicated only for user authentication. So, it should be separated from transactional database.
How to do that?
Thank you.
In short...yes it is possible for you to use a different database for your authentication. You would need to create a second database configuration in your application/config/database.php file (similar to the below) in addition to your default config.
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "db_name";
$db['default']['dbdriver'] = "mysql";
$db['authentication']['hostname'] = "localhost";
$db['authentication']['username'] = "root";
$db['authentication']['password'] = "";
$db['authentication']['database'] = "auth_db_name";
$db['authentication']['dbdriver'] = "mysql";
For further reference see - http://ellislab.com/codeigniter/user-guide/database/configuration.html
You would then have to modify your ion_auth model to use this second db configuration, which you would set when loading the database.
$auth_db = $this->load->database('authentication', TRUE);
Then change all database queries in the model replacing $this->db with $auth_db.
So, $this->db->select('password, salt') would become $auth_db->select('password, salt').
For further reference see - http://ellislab.com/codeigniter/user-guide/database/connecting.html
Hope that helps!
The simplest way would be to expand on what MY_Mark said.
Create the new DB config
$db['authentication']['hostname'] = "localhost";
$db['authentication']['username'] = "root";
$db['authentication']['password'] = "";
$db['authentication']['database'] = "auth_db_name";
$db['authentication']['dbdriver'] = "mysql";
Then in the constructor of the ion_auth_model.php do this:
$this->db = $this->load->database('authentication', TRUE);
And it should just work after that.
I have a database and a user which is created through cpanel(i have added that user to that database and i am able to access that database using that user and its password. i have given all privileges to that user through cPanel(by clicking on the username in 'current database' table in cpanel and clicking on all privileges check box ).
In my application(which is created using PHP- Codeigniter framework) i want to create new database and tables in that database as per users requirement. For that i am using Codeigniter dbforge class for creating database and the tables in that.
$this->load->dbforge();
if ($this->dbforge->create_database($database_name)){
$config['hostname'] = "localhost";
$config['username'] = "user_name";
$config['password'] = "users_password";
$config['database'] = $database_name;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = TRUE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$config['swap_pre'] = '';
$config['autoinit'] = TRUE;
$config['stricton'] = FALSE;
$db_new=$this->load->database($config,TRUE);
$this->db=$db_new;
$this->load->dbforge();
$fields=array(
'id'=>array('type'=>'INT','auto_increment'=>TRUE,'constraint'=>'5'),
'first_name'=>array('type'=>'VARCHAR','constraint'=>'60'),
'last_name'=>array('type'=>'VARCHAR','constraint'=>'60','null' => TRUE),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('id',TRUE);
$this->dbforge->create_table('clients');
}
In the above code i am creating a database first and then loading that database as the current database . This code works perfectly in local host WAMP server but when i uploaded it to the server and then tried to create the database it gives the following Error
Error No 1044: Access denied for user 'username'#'localhost' to database 'database_name'
The line of code which caused thr error is
if ($this->dbforge->create_database($database_name)){
*In that database config setting shown in the above code the 'username' and 'password' is given as the username and password of the already existing database(which is created through cPanel) . Is that the problem...?* .
the current user using which we are trying to create new database is having all the avilable privileges. but in that 'CREATE DATABASE ' privilege is not there.
For database naming I have used the ' prefix_ ' in the database name(CPanel database naming format)
How can i create the database using dbforge class and access to that database using a existing user ...?
Try Create a Super User first
mysql > GRANT ALL PRIVILEGES on databasename.* TO "username"#"localhost" IDENTIFIED BY "password";