php CodeIgniter 错误 - 无法使用提供的设置连接到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22533644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:05:46  来源:igfitidea点击:

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

phpmysqlcodeigniter

提问by AppleBud

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

我是 PHP 和 CodeIgniter 的新手,我正在尝试使用 codeIgniter 的 MVC 模式从 mysql 表中获取数据。

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:

我的database.php文件是:

  $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.

我的 mysql 已启动并正在运行,但我仍然无法找出此错误的原因。

回答by Chococroc

Just for debugging your problem:

仅用于调试您的问题:

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

转到system/database/mysql/mysql_driver并在db_connect方法中删除@ 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.

这将向您显示 ddbb 连接有什么问题,现在您将了解问题所在。之后,发布MySQL服务器错误。

回答by lulu

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

database.php在主机上设置文件的用户名和密码,没关系。

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

回答by Jimbo

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/

只需运行您的 mysql 服务器。我有这样的错误。然后我运行我的 mysql 服务器,如果你已经安装了它,请转到 services -> mysql/mysqld/mysql2/

or anything like mysql.. select automatic and start.

或任何类似 mysql.. 选择自动并启动。

回答by Fortran

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

首先在 database.php 末尾使用此脚本调试您的数据库连接

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

Or you can change

或者你可以改变

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

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

in /application/config/database.php

在/application/config/database.php

回答by sotoz

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).

我知道这可能是评论,但我不能评论(还)。