0

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_ci

Filename: 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.

enter image description here

Please advise me if there is any other way to fix it or if I have made any mistake.

Thanks in advance.

3
  • I posted about this issue on Codeigniter forum, One of them answered me to follow the code to create a new database. But this is not working as well. // 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!'; } Commented Jun 9, 2022 at 3:59
  • One more idea I get from codeigniter forum user is "you need to create a seperate config for a new database in app/Config/Database.php otherwise Database Forge, or it will use the default database group." public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => '', 'password' => '', 'database' => '', ]; Commented Jun 12, 2022 at 15:29
  • I posted on facebook codeigniter groups about this. But one of them said, as I am using cpanel and shared hosting, I need permission to create database. But they will not give it due to shared server. Commented Jun 13, 2022 at 3:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.