I have a accounting management project where I need to store some specific data to a new database. I am fetching a issue when I am going to create a new database in server. It is working fine in local. As this project is made with Codeigniter 3, I am using codeignite dbforge() library to create new databse.
I have made this following this documentation of codeigniter 3. https://codeigniter.com/userguide3/database/forge.html#database-forge-class
// First Code
if (!$this->dbutil->database_exists($newDb)){
$this->dbforge->create_database($newDb);
}
But when I run this code, I get this error message.
Error Number: 1044
Access denied for user 'shohowqj_testing_usr'@'localhost' to database 'shohoz_hishab_backup_2022'
CREATE DATABASE
shohoz_hishab_backup_2022
CHARACTER SET utf8 COLLATE utf8_general_ciFilename: modules/yearly_closing/controllers/Yearly_closing.php
Line Number: 68
Here "shohowqj_testing_usr" is my current username of database.
Then I researched on google and stackoverflow. I tried with some other way to fix it. But it did not work.
I found this question and answer of stackoverflow and I implemented accourdingly. But it is yet not working.
Codeigniter - database creation using $this->dbforge->create_database() not working in server
This is my code
$newDb = 'shohoz_hishab_backup_'.date('Y');
$db_user = 'new_user';
$db_password = 'new_password';
if (!$this->dbutil->database_exists($newDb)){
$this->dbforge->create_database($newDb);
$this->db->query("CREATE USER '". $db_user ."'@'localhost' IDENTIFIED BY '". $db_password ."';");
$this->db->query("GRANT ALL PRIVILEGES ON ".$newDb.".* TO '". $db_user ."'@'localhost';");
}
This code is showing the same error message.
I am using cpanel in my server. This error is showing in server only. But it is working fine on local machine.
I have given the full permission of my existing user named "shohowqj_testing_usr". See this image bellow.
Please advise me if there is any other way to fix it or if I have made any mistake.
Thanks in advance.
// You can also pass another database object to the DB Forge loader, // in case the database you want to manage isn’t the default one: $this->myforge = $this->load->dbforge($this->other_db, TRUE); // NOTE: my_db is a database configuration file in database.php // not the default one. if ($this->dbforge->create_database('my_db')) { echo 'Database created!'; }
public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => '', 'password' => '', 'database' => '', ];