【问题标题】:CodeIgniter: Unable to connect to your database server using the providedCodeIgniter:无法使用提供的连接到您的数据库服务器
【发布时间】:2016-07-06 19:36:42
【问题描述】:

发生数据库错误

无法使用提供的设置连接到您的数据库服务器。

文件名:libraries/Session/Session.php

行号:102

我是 CI 的新手,我在我的网站上启动 Db 时遇到了这个错误,我不知道如何解决。

你能解决这个错误吗?

应用程序/配置/数据库.php

    $active_group = 'default';
    $query_builder = TRUE;
    require_once('mainconfig.php');
 $db['default'] = array(
        'dsn'   => '',
        'hostname' => $config['mysql_host'],
        'username' => $config['mysql_username'],
        'password' => $config['mysql_password'],
        'database' => $config['mysql_database'],
        'dbdriver' => $config['mysql_driver'],
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => $config['mysql_charset'],
        'dbcollat' => $config['mysql_dbcollat'],
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
);

应用程序/config/mainconfig.php

 defined('BASEPATH') OR exit('No direct script access allowed');
 $config['mysql_host'] = 'localhost';
 $config['mysql_username'] = 'xxxcc';
 $config['mysql_password'] = 'xxxcc';
 $config['mysql_database'] = 'xxccv';
 $config['mysql_driver'] = 'mysqli';
 $config['mysql_charset'] = 'utf8';
 $config['mysql_dbcollat'] = 'utf8_general_ci';
 $config['base_js'] = 'assets/js';
 $config['base_css'] = 'assets/css';
 $config['base_plugins'] = 'assets/plugins';
 $config['base_images'] = 'assets/images';
 $config['base_fonts'] = 'assets/font-awesome';
 $config['other_base'] = 'assets/fonts';
 $config['email_author'] = 'admin@localhost';
 $config['limit'] = 7;

【问题讨论】:

  • 检查 application/config/database.php 中的数据库连接

标签: php mysql codeigniter mysqli connection


【解决方案1】:

如果您使用 codeigniter 3,请使用最新版本的 php。一旦我收到这些错误,然后我更新了我的 php,然后在我的项目上成功设置 codeigniter 后它就可以工作了。

请按照您的应用程序/config/database.php 进行操作

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'db_bdtimes', // your database name
    '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
);

【讨论】:

  • 我正在使用 php 5.5,我必须更新到 5.6?
  • 让我们试一试!但是你有没有为你的项目建立一个数据库?
  • 我尝试过,但失败了。
  • 在这里设置你的数据库名 'database' => 'db_bdtimes', // 你的数据库名
  • 不,我在mainconfig.php中设置了数据库
【解决方案2】:

只需更改,它肯定会起作用

$db['default']['hostname'] = 'localhost';

$db['default']['hostname'] = '127.0.0.1';

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2020-01-11
    • 2012-03-21
    • 1970-01-01
    • 2018-01-19
    • 2016-02-02
    相关资源
    最近更新 更多