Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

php - CI - show database error or fail

I've developed a simple login system which works ok but fails, and I need to know why

QUESTION: How to show what is causing the fail. This is not a validation error but an error either with the data being passed to MySQL or the query somehow failing

here's the db function:

function login($email,$password)
{
    $this->db->where("email",$email);
    $this->db->where("password",$password);

    $query=$this->db->get("users");
    if($query->num_rows()>0)
    {
        foreach($query->result() as $rows)
        {
            //add all data to session
            $newdata = array(
                'user_id'  => $rows->id,
                'user_name'  => $rows->username,
                'user_email'    => $rows->email,
                'logged_in'  => TRUE,
            );
        }
        $this->session->set_userdata($newdata);
        return true;
    }
    return false;
}

And here's the logic:

public function login()
{
    $this->load->library('form_validation');
    // field name, error message, validation rules
    $this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');

    if($this->form_validation->run() == FALSE)
    {
        $this->signin();
    }
    else
    {
        $email=$this->input->post('email');
        $password=md5($this->input->post('pass'));
        $result=$this->user_model->login($email,$password);
        if($result)
        {
            $this->dash();
        }
        else
        {
            $data['title']= 'Login Error';
            $this->load->view('nav/header', $data);
            $this->load->view('login', $data);
            $this->load->view('nav/footer', $data);
        }
    }
}

I know the error is happening as I redirect back to login page if fail and change title text to show me (only in testing mode for right now) - but how can I find out what is going wrong with the query?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use log_message and check the logs if the y behave as expected:

http://ellislab.com/codeigniter/user-guide/general/errors.html

I usually just use echo '<pre>'; print_r($query->result());die; just after the $query is formed. It's faster.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...