0

我正在使用数据库开发codeigniter项目,它在本地主机上运行良好,但是当我将它上传到真实服务器上时,发生了很多问题。

我真的做了很多搜索,但我找不到解决方案。

当我从数据库中检索数据并给我这个错误时,result_array() 函数不起作用" Fatal error: Call to a member function result() on a non-object in /home/mostafa/public_html/Sales/application/models/m_login.php on line 13 "

这是我的控制器代码

class c_login extends CI_Controller {
public function index() {
    $this->load->view('login');
}

public function signin(){
    $username = $this->input->post('username');
    $password = $this->input->post('password');

    $this->load->model('m_login');
    $user = $this->m_login->signin($username, $password);

    if(!$user){
        echo "<script>alert('Your Username Or Password is Incorrect');</script>";
    }else{

        //set session
        $userData = array(
            'id' => $user[0]['id'],
            'name'  => $user[0]['name'],
            'admin' => $user[0]['admin']
        );

        $this->session->set_userdata($userData);

        if ($user[0]['admin'] == 1) {
            redirect('c_home_admin');
        }else{
            redirect('c_home_user');
        }
    }
}

}

这是模型代码

class m_login extends CI_Model{
function insertUser($data) {
    $this->db->insert('users', $data);
}

public function signin($username, $password){
    $this->db->select('*')->from('users')->where(array('username' => $username, 'password' => $password));
    $query = $this->db->get();
    return $query->result_array();
}}

这是 database.php 配置:

$active_group = 'default';

$active_record = TRUE;

$db['default']['hostname'] = 'localhost';

$db['default']['username'] = 'root';

$db['default']['password'] = '**********';

$db['default']['database'] = 'mydb';

$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';

$db['default']['swap_pre'] = '';

$db['default']['autoinit'] = TRUE;

$db['default']['stricton'] = FALSE;

提前致谢

4

1 回答 1

0

我要检查的第一件事是您的数据库模式在开发时是否与实时时完全相同。如果表格不同(例如缺少字段),您将收到该错误。

编辑

尝试如下重写您的 SQL 查询:

$sql = "
    SELECT * FROM `users` where `username` = ? AND `password` = ?
";
$q = $this->db->query($sql, array($username, $password));
return $q->result_array();

(对不起 - 在我的答案中发布它,因为评论看起来很不稳定......)

于 2013-11-14T11:23:31.240 回答