Php CodeIgniter与数据库的连接
我决定结束与条令2.3的合作,现在我想只使用CodeIgniter连接到数据库。你们知道我的代码哪里有问题吗 我得到的错误是:Php CodeIgniter与数据库的连接,php,codeigniter,Php,Codeigniter,我决定结束与条令2.3的合作,现在我想只使用CodeIgniter连接到数据库。你们知道我的代码哪里有问题吗 我得到的错误是: A PHP Error was encountered Severity: Notice Message: Undefined property: Hello::$db Filename: core/Model.php Line Number: 51 Database.php $db['default']['hostname'] = 'localhost:80
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Hello::$db
Filename: core/Model.php
Line Number: 51
Database.php
$db['default']['hostname'] = 'localhost:8080';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'ci_doctrine';
$db['default']['dbdriver'] = 'mysql';
$autoload['libraries'] = array('database');
控制器
<?php
// system/application/controllers/hello.php
class Hello extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function world() {
echo "Hello CodeIgniter!";
}
function user_test() {
$this->load->model('user');
$this->user->save_User('username','password','first_name','last_name');
}
}
?>
<?php
// system/application/models/user.php
class User extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function save_User($username,$password,$fName,$lName) {
$this->username = $username;
$this->password = $password;
$this->first_name = $fName;
$this->last_name = $lName;
$this->db->insert('user', $this);
}
}
?>
模型-数据库“用户”中的表名
问题在于您没有加载数据库类
$this->load->database();
是Codeigniter用户指南
如果不连接,则无法使用数据库类,因此它表示未定义属性db。您需要加载数据库库: config/autoload.php
$autoload['libraries'] = array('database');
用于手动连接在控制器文件中使用此选项
$this->load->database();
用于数据库中的自动连接
转到Controller,然后在autoload.php中进行以下更改
$autoload['libraries'] = array('database');
您需要加载数据库库: config/autoload.php
$db['default']['hostname'] = 'localhost:8080';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'ci_doctrine';
$db['default']['dbdriver'] = 'mysql';
$autoload['libraries'] = array('database');
试试看。您在本地主机上吗?如果是,则尝试移除8080post@raheel但如果我删除这个端口,我将无法连接到该网站。我的端口80有Skype,WAMP有8080。你是说->$autoload['config']=database.php??是的,您正在加载数据库类吗?你还没有提到它。不,我是说$autoload['libraries']=array('database');文件夹应用程序/config文件autoload.php:)我得到了这个->发生了一个数据库错误,无法使用提供的设置连接到数据库服务器。文件名:C:\wamp\www\nauka\system\database\DB\u driver.php行号:124现在,您的应用程序/config/database.php文件中出现另一个错误。将您的规格更改为正确的规格,以便连接到您的数据库。尝试通过转到Skype->tool->settings->advance->connection来释放wamp的后期编辑,取消选中use port 80和443,然后重新启动wamp和Skype