如何使用 CodeIgniter 测试数据库连接?

发布于 2024-10-06 03:27:06 字数 1577 浏览 1 评论 0原文

我正在尝试学习 CodeIgniter,但不幸的是,他们在 wiki 上列出的许多教程已有几年历史,并且根据其他人的评论,不适用于最新版本的 CodeIgniter。

我确实找到了一个没有任何负面评论的 - IBM DeveloperWorks - 但我无法让它工作。表单提交之前的一切都很好,但是提交后我得到一个空白页面,并且没有任何内容发送到数据库。

如何在 CodeIgniter 中测试/排除数据库连接故障?我知道我的设置(就主机、数据库名、用户名/密码等而言)是正确的,因为我在普通的 PHP 站点上成功使用了它们。

编辑添加:或者,任何人都可以向我指出适用于最新版本的最新初学者教程吗?我不需要 MVC 教程;我需要的是 MVC 教程。我熟悉设计模式。我只需要学习CodeIgniter。

编辑以添加database.php文件:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$active_group = "default";
$active_record = TRUE;

//$db['default']['hostname'] = "localhost";
$db['default']'hostname'] = "myHostName.powwebmysql.com";
$db['default']['username'] = "myUserName";
$db['default']['password'] = "myPassword";
$db['default']['database'] = "codeigniter"; //yes, database is called codeigniter
$db['default']['dbdriver'] = "mysql";
$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";


/* End of file database.php */
/* Location: ./system/application/config/database.php */

编辑:将数据库加载添加到构造函数时出现错误消息:

A PHP Error was encountered
Severity: Notice

Message: Undefined property: Welcome::$load
Filename: controllers/welcome.php

Line Number: 6

其中第6行是$this->load->database();

I'm trying to learn CodeIgniter, but unfortunately many of the tutorials they list on their wiki are several years old and, based on others' comments, don't work on the newest version of CodeIgniter.

I did find one that didn't have any negative comments - on IBM DeveloperWorks- but I can't get it to work. Everything up until the form submit is fine, but after I submit I get a blank page and nothing is sent to the database.

How can I test/troubleshoot a database connection in CodeIgniter? I know my settings (as far as host, dbname, username/password, etc.) are correct because I'm using them successfully with a plain vanilla PHP site.

Edit to add: alternatively, can anyone point me to a recent beginner tutorial that works with the recent version? I don't need an MVC tutorial; I'm familiar with the design pattern. I just need to learn CodeIgniter.

Edit to add database.php file:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$active_group = "default";
$active_record = TRUE;

//$db['default']['hostname'] = "localhost";
$db['default']'hostname'] = "myHostName.powwebmysql.com";
$db['default']['username'] = "myUserName";
$db['default']['password'] = "myPassword";
$db['default']['database'] = "codeigniter"; //yes, database is called codeigniter
$db['default']['dbdriver'] = "mysql";
$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";


/* End of file database.php */
/* Location: ./system/application/config/database.php */

EDIT: error message when adding database load to constructor:

A PHP Error was encountered
Severity: Notice

Message: Undefined property: Welcome::$load
Filename: controllers/welcome.php

Line Number: 6

where line 6 is $this->load->database();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网白 2024-10-13 03:27:06

请使用下面的代码:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'myUserName';
$db['default']['password'] = 'myPassword';
$db['default']['database'] = 'myDatabaseName';


/** Do not change the below values. Change only if you know what you are doing */
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = "utf8"; 
$db['default']['dbcollat'] = "utf8_unicode_ci";
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$active_group = 'default';
$active_record = TRUE;

Please use below code:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'myUserName';
$db['default']['password'] = 'myPassword';
$db['default']['database'] = 'myDatabaseName';


/** Do not change the below values. Change only if you know what you are doing */
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = "utf8"; 
$db['default']['dbcollat'] = "utf8_unicode_ci";
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$active_group = 'default';
$active_record = TRUE;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文