我想为我的应用程序创建一个简单的安装程序。我创建了一个名为" installer" 的库,它将检查数据库'数据库' => 变量。如果未定义数据库字段,它将重定向到安装文件夹。这是我的剧本:
<?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
);
答案 0 :(得分:0)
错误&#34; mysqli :: real_connect():( HY000 / 1045):拒绝用户访问&#39;&#39; localhost&#39; (使用密码:否)&#34;您已经设置了#34;数据库&#34;自动加载配置中的库,未在数据库配置文件中提供数据库主机,用户名和密码。
您可以尝试删除&#34;数据库&#34;从autoload配置和检查数据库详细信息,方法是加载数据库配置并在脚本中检查其值,如下所示:
git add .
git commit -m "changed xyz"
git push (name of branch?)
答案 1 :(得分:0)
请在database.php中设置实时服务器的详细信息,这不应该是空白的。
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
答案 2 :(得分: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。希望它有所帮助。
答案 3 :(得分: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
);