Codeigniter库正在使用localhost但不在Web服务器上

问题描述 投票:2回答:4

我想为我的应用程序创建一个简单的安装程序。我创建了一个名为“installer”的库,它将检查数据库'database'=>变量。如果未定义数据库字段,它将重定向到安装文件夹。这是我的脚本:

<?php
class Installer {
    public function __construct() {
        //get the CI instance
        $CI =& get_instance();
        $CI->load->database();


    //check if databse config set
       if ($CI->db->database == "") {
            redirect('install');
        } else {
        if (is_dir('install')) {
                echo '<i>Plese delete or rename <b>Install</b> folder</i>';
                exit;
            }
        }
    }
}

我已经使用数据库和会话库在自动加载中设置了库

$autoload['libraries'] = array('database','session','installer');

它在我的localhost上运行良好。但是当我第一次将它上传到我的网络服务器时遇到404错误(因为控制器文件的第一个字母应该是资本),那么我已经纠正了这一点。但当我到达我的网址http://example.com/demo(我在public_html / demo /目录中的文件)时,它显示出一些错误,如

A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: mysqli/mysqli_driver.php

Line Number: 109

Backtrace:

File: /home/xxxxx/public_html/demo/application/controllers/Home.php
Line: 25
Function: __construct

File: /home/xxxxx/public_html/demo/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: mysqli::real_connect(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO)

Filename: mysqli/mysqli_driver.php

Line Number: 161

Backtrace:

File: /home/xxxxx/public_html/demo/application/controllers/Home.php
Line: 25
Function: __construct

File: /home/xxxx/public_html/demo/index.php
Line: 292
Function: require_once


A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/xxxxx/public_html/demo/system/core/Exceptions.php:272)

Filename: core/Common.php

Line Number: 568

Backtrace:

File: /home/xxxxxx/public_html/demo/application/controllers/Home.php
Line: 25
Function: __construct

File: /home/xxxxxx/public_html/demo/index.php
Line: 292
Function: require_once

A Database Error Occurred

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

Filename: controllers/Home.php

Line Number: 25

当我使用它localhost它运作良好。但是当我在我的服务器上使用它时,我收到了这个错误。我的数据库配置文件:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '',
    'username' => '',
    'password' => '',
    'database' => '',
    '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 codeigniter
4个回答
0
投票

请在database.php中设置实时服务器的详细信息,这不应该是空白的。

'hostname' => '',
'username' => '',
'password' => '',
'database' => '',

0
投票

错误“mysqli :: real_connect():( HY000 / 1045):访问被拒绝用户''@ localhost'(使用密码:NO)”你得到了指向错误的数据库访问细节,因为你设置了“数据库”自动加载配置中的库,未在数据库配置文件中提供数据库主机,用户名和密码。

您可以尝试从autoload配置中删除“database”并通过加载数据库配置并在脚本中检查其值来检查数据库详细信息,如下所示:

  $CI->config->load('database', TRUE);
  $dbConfig = $CI->config->item('database');

0
投票

CodeIgniter有一个配置文件,允许您存储数据库连接值(用户名,密码,数据库名称等)。配置文件位于application / config / database.php。您还可以通过将database.php放在相应的环境配置文件夹中来为特定环境设置数据库连接值。

因此,您需要将数据库凭据保存在该文件中以使其正常工作。

像这样:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost', // The hostname of your database server. Often this is ‘localhost’.
    'username' => 'root', //user name of the database account
    'password' => '', // password
    'database' => 'database_name', // name of the database
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array()
);

请参阅他们的手册here。希望能帮助到你。


0
投票

编辑你的C:\xampp\htdocs\.. application\config\database.php

$active_group = 'default';

$query_builder = TRUE;

$db['default'] = array(

    'dsn'   => '',

    'hostname' => 'localhost',

    'username' => 'root',

    'password' => '',

    'database' => 'your_database_name',

    'dbdriver' => 'mysqli',

    'dbprefix' => '',

    'pconnect' => TRUE,

    '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

);
© www.soinside.com 2019 - 2024. All rights reserved.