Welcome Guest, Not a member yet? Register   Sign In
Creating a new database is not working at server in codeigniter 3
#11

@InsiteFX
Which username and password should I supply to create a new database using code please? I have supplied both my running database user username and password and cpanel username and password. But it did not work.

Can you please show me what exactly you are saying?
Thank you
Using Codeigniter since 2016 ( skype: imranertaza )
Reply
#12

(This post was last modified: 05-21-2022, 10:26 PM by InsiteFX.)

You can test your database connection using this code.

PHP Code:
<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php

$dbname 'name';
$dbuser 'user';
$dbpass 'pass';
$dbhost 'host';

$connect mysqli_connect($dbhost$dbuser$dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($connect$dbname) or die("Could not open the db '$dbname'");

$test_query "SHOW TABLES FROM $dbname";
$result mysqli_query($connect$test_query);

$tblCnt 0;
while(
$tbl mysqli_fetch_array($result)) {
  $tblCnt++;
  // echo $tbl[0]."<br />\n";
}

if (!
$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";


For dbForge you can try this.

PHP Code:
// 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_dbTRUE);

// 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!';


I think your problem is that your trying to create a new database but it is using the default database settings.
Please let us know how you make out on this issue.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#13

(This post was last modified: 05-22-2022, 11:24 PM by webdeveloper011.)

@InsiteFX
Hi
if you see this post.
https://stackoverflow.com/questions/7223...eigniter-3

I have tried creating a new databse with new user. I have created new user with giving all permission so that this user can have access to this.

Here is the code:


PHP 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';");



Thank you
Using Codeigniter since 2016 ( skype: imranertaza )
Reply
#14

As I said before 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.

PHP Code:
    /**
    * The default database connection.
    *
    * @var array
    */
    public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => '',
        'password' => '',
        'database' => '',
        'DBDriver' => 'MySQLi',
        'DBPrefix' => '',
        'pConnect' => false,
        'DBDebug'  => (ENVIRONMENT !== 'production'),
        'charset'  => 'utf8',
        'DBCollat' => 'utf8_general_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'    => 3306,
    ]; 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#15

@InsiteFX
ok, If I have to create a seperate config for a new database, I have to create a new database manually with user and password using cpanel. right?
Then I have to set the new config for the new database in the app/config/database.php

After that I can take that yearly closing to the new database. This is what you are saying. Am I right?
Thank you
Using Codeigniter since 2016 ( skype: imranertaza )
Reply
#16

You should be able to create a new database using Database Forge using your admin login and password..

Try using those in the new database config file and see if you can create it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#17

@InsiteFX
Hi
I tried to create new database using Database Forge using my admin login and password as well. But it is showing the same error.
Using Codeigniter since 2016 ( skype: imranertaza )
Reply
#18

Are you able to add a new database in cPanel?
Either using the cPanel MySQL Databases section or in cPanels phpMyAdmin....
Reply
#19

(This post was last modified: 06-08-2022, 09:06 PM by webdeveloper011.)

@JustJohnQ
No. Not yet. It did not work still now. If you find any solution for that, please help me.
Thank you

@InsiteFX

Hi
you told me to create a seperate config for a new database in app/Config/Database.php

Do I have to create that before using the Database Forge?
Thank you
Using Codeigniter since 2016 ( skype: imranertaza )
Reply
#20

It maybe that your hosting provider only allows the databases to be created in CPanel.

My provider allows me to create them through code.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB