php CodeIgniter 连接到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14665018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter connection to database
提问by Rafa? Developer
I decided end my cooperation with Doctrine 2.3 and now I would like to make connection to database using only CodeIgniter have you got idea where is problem with my code?
我决定结束与 Doctrine 2.3 的合作,现在我想只使用 CodeIgniter 连接到数据库你知道我的代码哪里有问题吗?
This Error I get:
我得到这个错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Hello::$db
Filename: core/Model.php
Line Number: 51
Database.php
数据库.php
$db['default']['hostname'] = 'localhost:8080';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'ci_doctrine';
$db['default']['dbdriver'] = 'mysql';
Controller
控制器
<?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');
}
}
?>
Model - table name in database "user"
模型 - 数据库“用户”中的表名
<?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);
}
}
?>
采纳答案by mallix
You need to load the database library:
您需要加载数据库库:
config/autoload.php
配置/自动加载.php
$autoload['libraries'] = array('database');
回答by Muhammad Raheel
回答by hassan javaid
For manual connection
Use this in your controller file
对于手动连接
在您的控制器文件中使用它
$this->load->database();
For automate connection in database
用于数据库中的自动连接
Go to Controller then in autoload.php make the following changes
转到 Controller 然后在 autoload.php 中进行以下更改
$autoload['libraries'] = array('database');
回答by hassan javaid
You need to load the database library:
您需要加载数据库库:
config/autoload.php
配置/自动加载.php
$autoload['libraries'] = array('database');
Try it .
尝试一下 。

