mysqli::real_connect(): (HY000/1049): Unknown database error in codeigniter

前端 未结 3 2040
天涯浪人
天涯浪人 2021-01-07 04:16

I\'m new to Codeigniter PHP framework. When I\'m testing my application I get \'Unknown database db_name\' error. I have browsed through several sites but didn\'t found solu

相关标签:
3条回答
  • 2021-01-07 04:41

    I was getting same error when i installed new wampserver 3.2.0, it is an evil it installs MariaDB by default and port 3306 is assigned to it , you cannot even change it. I think codeigniter/php tries to connect to this port by default. I used following , it worked for me i.e. hostname => 'localhost:3308'

     $db['default'] = array(
            'dsn'   => '',
            'hostname' => 'localhost:3308',
            'username' => 'root',
            'password' => '',
            'database' => 'soft',
            'dbdriver' => 'mysqli',
            'dbprefix' => '',
            'pconnect' => FALSE,
            'db_debug' => (ENVIRONMENT !== 'production'),
            'cache_on' => FALSE,
            'cachedir' => '',
            'char_set' => 'utf8',
            'dbcollat' => 'utf8_general_ci',
            'swap_pre' => '',
            'encrypt' => FALSE,
            'compress' => FALSE,
            'stricton' => FALSE,
            'failover' => array(),
            'save_queries' => TRUE
        );
    
    0 讨论(0)
  • 2021-01-07 04:49

    I was too getting this error. There are 2 fields in application/config/database.php file that should match up with your actual database:

    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    

    Make sure, the actual DB is of the same type & char_set as mentioned in the above file.

    0 讨论(0)
  • 2021-01-07 04:53

    That's what i did to handle whether db exists or not :

    First Change this in application/config/database.php

    'db_debug' => (ENVIRONMENT !== 'production')
    

    to

    'db_debug' => FALSE; 
    

    Then add these two in your controller

        $this->load->dbforge();
        $this->load->dbutil();
    

    Then check it in your method

        if( $this->dbutil->database_exists($this->db->database))
        {
            echo 'Database Already Exists';
        }
        else
        {
            if($this->dbforge->create_database($this->db->database))
            {
                echo 'Database created successfully !';
            }else
            {
                print_r($this->db->error());
                /*
                   Array ( 
                         [code] => 1007 
                         [message] => Can't create database 'my_db'; database exists 
                        )
               */
            }
        }
    
    0 讨论(0)
提交回复
热议问题