【问题标题】:Codeigniter library is working on localhost but not on web serverCodeigniter 库在 localhost 上工作,但不在 Web 服务器上
【发布时间】:2016-01-12 04:58:47
【问题描述】:

我想为我的应用程序创建一个简单的安装程序。我创建了一个名为 "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');

它在我的本地主机上运行良好。但是当我第一次将它上传到我的网络服务器时,我得到了 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

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

$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


    【解决方案1】:

    请在database.php中设置实时服务器的详细信息,这不能为空。

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

    【讨论】:

    • 如果我设置了所有详细信息,那么为什么我需要安装程序库和额外的代码来设置用户提交的数据库详细信息到数据库配置文件?
    【解决方案2】:

    错误“mysqli::real_connect(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO)”你得到指向错误的数据库访问详细信息,因为你已经设置自动加载配置中的“数据库”库,并且未在数据库配置文件中提供数据库主机、用户名和密码。

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

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

    【讨论】:

    • 我已经提到这个脚本在我的 wamp 服务器上运行良好,没有错误。但问题出在实时服务器上。
    • 我已经更新了我的答案,如果对你有帮助,请检查一下。
    【解决方案3】:

    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。希望对您有所帮助。

    【讨论】:

    • 不想存储,如果我将数据库名称设置为数据库配置文件,那么我的库永远不会重定向到安装文件夹。最初我希望它空白,以便我的图书馆很容易知道这是新安装。 N.b 我知道如何将数据库信息设置为数据库配置文件
    • “库从不重定向到安装文件夹”是什么意思?
    • “安装程序”是一个库。我将其设置为自动加载
    【解决方案4】:

    编辑您的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
    
    );
    

    【讨论】:

    • 虽然只有代码回答可能会解决问题,但一些解释对于理解解决方案以及将来如何解决类似问题大有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多