CodeIgniter error- unable to connect to database using the provided settings

泪湿孤枕 提交于 2020-01-03 03:38:05

问题


I am new to PHP and CodeIgniter and I am trying to fetch data from a mysql table using MVC pattern of codeIgniter.

My model class is:

<?php

class News_Model extends CI_Model
{

    public function _construct()
    {
        $this->load->database();
    }

    public function get_news($id)
    {
        if($id!=FALSE)
        {
            $query= $this->db->get_where('news', array('id' => $id));
            return $query->row_array();
        }
        else
        {
            return FALSE;
        }   
    }

}


 ?>

and my database.php file is:

  $active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'user';
$db['default']['dbdriver'] = 'mysql';
$db['default']['port'] = '3306';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

But when I run my file, it shows the following error:

Unable to connect to your database server using the provided settings.

Filename: C:\wamp\www\codeignite\system\database\DB_driver.php

Line Number: 124

My mysql is up and running but still i am not able to figure out the cause for this error.


回答1:


Just for debugging your problem:

Go to system/database/mysql/mysql_driver and in db_connect method delete the @ from @mysql_connect($this->hostname, $this->username, $this->password, TRUE);

That will show you what is wrong with the ddbb connection, and you'll now what is the problem. After that, post the MySQL server error.




回答2:


Don't know if it is a copy paste error or it really matters on your problem but you have only one underscore at the model constructor.

You should try it like that

 public function __construct()

I know this could be a comment but I can't comment (yet).




回答3:


First debug your database connection using this script at the end of database.php

echo '<pre>';
print_r($db['default']);
echo '</pre>';

Or you can change

$db['default']['pconnect'] = TRUE; --> $db['default']['pconnect'] = FALSE;

in /application/config/database.php




回答4:


Please set username and password for file database.php on hosting, it's ok.

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'abaccc';
$db['default']['password'] = 'abaccc123';
$db['default']['database'] = 'abaccc';
$db['default']['dbdriver'] = 'mysql';



回答5:


Just run your mysql server. i have an error like that. and i run my mysql server, if you are already installed it, please go to services -> mysql/mysqld/mysql2/

or anything like mysql.. select automatic and start.



来源:https://stackoverflow.com/questions/22533644/codeigniter-error-unable-to-connect-to-database-using-the-provided-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!