gpt4 book ai didi

php - CodeIgniter 连接到数据库

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:08 24 4
gpt4 key购买 nike

我决定结束与 Doctrine 2.3 的合作,现在我想只使用 CodeIgniter 连接到数据库,你知道我的代码哪里有问题吗?

我得到这个错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Hello::$db

Filename: core/Model.php

Line Number: 51

数据库.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');

}
}

?>

模型 - 数据库“用户”中的表名

<?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);
}

}
?>

最佳答案

需要加载数据库库:

配置/自动加载.php

$autoload['libraries'] = array('database');

关于php - CodeIgniter 连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com