我刚刚在Windows 7机箱,IIS 7,快速CGI,无模块上设置了一个简单的PHP Code Igniter项目。
当我通过执行此类this->load->database()
在Model类的一个函数中加载数据库时,线程似乎停在该特定行上。没有后续操作完成。
class Account_model extends Model {
var $userId = '';
var $userName = '';
var $requestToken = '';
var $accessToken = '';
var $enabled = false;
var $startOfDay;
var $endOfDay;
function Account_model() {
parent::Model();
}
function get($userId) {
$this->load->database();
$query = $this->db->get_where('accounts', array('userId' => $userId), 1, 0);
return $query->result();
}
function insert() {
$this->load->database();
// ** stop **
$this->db->insert('accounts', $this); //never gets to this
}
}
如果我完全省略该行,我在模型中得到一个php异常未定义变量$ db。
来电控制器:
class SignUp extends Controller {
function SignUp() {
parent::Controller();
}
function createUser() {
echo 'processing';
$this->load->model('Account_model');
$this->Account_model->userId = 'asd';
$this->Account_model->userName = 'test_user_pls_delete';
$this->Account_model->enabled = true;
$this->Account_model->startOfDay = time();
$this->Account_model->insert();
echo 'done'; // never gets to this
}
}
我已验证数据库配置是否正确(主机名,驱动程序等),并且能够使用MySQL workbrench从计算机连接到数据库服务器。
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "{omitted}";
$db['default']['username'] = "{omitted}";
$db['default']['password'] = "{omitted}";
$db['default']['database'] = "{omitted}";
$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";
数据库表名:accounts
有关如何解决此问题的任何想法?
答案 0 :(得分:1)
嗯......这是我的模特......
class Account_model extends Model {
function Account_model() {
// Call the Model constructor
parent::Model();
$this->db = $this->load->database('default', TRUE);
}
function showTables() {
$query = "show tables";
$rs = $this->db->query($query);
return $rs->result_array();
}
在$ db中加载数据库,因此$ db可以处理查询。
答案 1 :(得分:1)
以下内容应该让你开始......
财务主任:
class SignUp extends Controller {
function SignUp(){
parent::Controller();
}
function createUser(){
echo 'processing';
$this->load->model('Account_model');
$result = $this->Account_model->insert($userId = 'asd', $userName = 'test_user', $enabled = 'true', $startOfDay = time());
echo ($result == TRUE) ? 'insert successful' : 'insert failed';
}
}
模特:
class Account_model extends Model {
function Account_model(){
parent::Model();
$this->db = $this->load->database('default', TRUE);
}
function add_user($userId, $userName, $requestToken = '', $accessToken = '', $enabled = 'false', $startOfDay = '', $endOfDay = '') {
$user = array(
'UserId' => $userId,
'UserName' => $userName,
'requestToken' => $requestToken,
'accessToken' => $accessToken,
'Enabled' => $enabled,
'startOfDay' => $startOfDay,
'endOfDay' => $endOfDay
);
$this->db->insert('accounts', $user);
return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
}
}
}
答案 2 :(得分:0)
您可以通过将“database”添加到autoload.php文件中的库数组来自动加载数据库,该文件位于:
application/config/autoload.php
或者,ad-hoc加载它。在这两种情况下,它都会使我们的配置位于:
application/config/database.php
您可以验证您的配置是否如上所述?