dqm83011 2016-07-24 22:21
浏览 36

为什么codeigniter在我的行上有记录时返回false?

why codeingiter is returning me null in this case null means false because I havent any record on the tabl, when I have records on my table. i'm new using this framework. I just want to display all my user with their passwords and type of user. by the way i'm creating a multi user login.

class Login extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->model('User_model');
    }

    public function index(){
            $data['module'] = "Home";
            $this->load->view('header',$data);
            $this->load->view('login');
    }

    public function getAccess(){
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $array = $this->User_model->login($username,$password);
        echo json_encode($array);
    } 

    public function logout(){
        $this->session->sess_destroy();
        redirect('login');
    }
}

model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

    public $variable;

    public function __construct(){
        parent::__construct();

    }

    public function login($username,$password){
        $query = $this->db->query("SELECT iduser,name,last_name,username,password,typeuser_idtypeuser from user INNER JOIN typeuser ON user.typeuser_idtypeuser = typeuser.idtypeuser WHERE username = '$username' AND password = '$password'");
        if($query->num_rows() > 0){
            return $query->result_array();
        }else{
            return false;
        }
    }

}
  • 写回答

2条回答 默认 最新

  • doulu4203 2016-07-24 22:33
    关注

    Modifies the model :

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class User_model extends CI_Model {
    
        public $variable;
    
        public function __construct(){
            parent::__construct();
    
        }
    
        public function login($username,$password){
            $query = "SELECT iduser,name,last_name,username,password,typeuser_idtypeuser from user INNER JOIN typeuser ON user.typeuser_idtypeuser = typeuser.idtypeuser WHERE username = ? AND password = ? LIMIT 1";
            $query = $this->db->query($query,array($username,$password));
            if($query->num_rows() != 0){
                return $query->result_array();
            }else{
                return false;
            }
        }
    
    }
    

    In your Controller verifies the variable $array before the echo:

    public function getAccess(){
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            if($array = $this->User_model->login($username,$password)) {
            echo json_encode($array);
            } else { echo 'NO DATA!'; }
        } 
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法