0
votes

In my codeigniter project, when the login button is clicked, it's not redirecting to the user home page. But for my client, it is working well.

When I use the print_r($query) it displays the following error:

CI_DB_mysql_result Object ( [conn_id] => Resource id #32 [result_id] => Resource id #38 > [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 0 [row_data] => )

I have tried result() at the end of my code, but it's still its not working. How do I solve this issue?

My Controller Code is as follows:

<?php 
class Main extends Controller {
   function Main()
   {
        parent::Controller();
   }

function index()
{

    $errMsg     = '' ;
        if($this->input->post('adminUsername') && $this->input->post('adminPassword')){
                    $user_name = $this->input->post('adminUsername');
                    $pass_word = base64_encode($this->input->post('adminPassword'));
                    $query=$this->db->get_where('gm_adminusers',array('adminUsername'=>$user_name,'adminPassword'=>$pass_word,'admin_status'=>'A'));
                    print_r($query);

                    if ($query->num_rows() > 0)
                    {
                            $arrRow =$query->row_array();
                            $newdata = array(
                             'sess_adminId'  =>$arrRow['adminId'],

                       );
                       $this->db_session->set_userdata($newdata);
                        if($this->db_session->userdata('sess_adminId')){
                                    redirect('user/home');
                                }
                                else{
                                    $errMsg ='<span style="color:red">Login Error :Please Check Your input.</span>';    
                                    /*redirect('user/home');    */
                                }   

                    }else{
                    $errMsg ='<span style="color:red">Critical Error:Contact Your Administrator</span>';    


                    }
    }

    $data['errMsg']         = $errMsg;
    $this->load->view('header');
    $this->load->view('index',$data);
    $this->load->view('footer');

}

enter image description here

enter image description here

1
Your query is returning 0 rows. That's not an "error". P.S. are you sure you typed in the right password? ;) P.P.S. I hope you don't actually think base64_encode is secure. - Rocket Hazmat
You have no error there. The query doesn't return any result. - Miguel G. Flores
FYI base64_encode is NOT a safe/secure way to hash passwords. CodeIgniter includes libraries to help deal with this, and there are many other libraries out there built for the purpose (bcrypt). Please see ellislab.com/codeigniter%20/user-guide/libraries/… and stackoverflow.com/a/7045061/183254 - stormdrain
Your SQL query is returning zero rows, to help debug this you can turn on the profiler by including $this->output->enable_profiler(TRUE); in your controller so that you can see the queries being run on your database. You can then check to query to debug why it is returning zero rows. - Bad Wolf
Your "admin_password" is stored as 123456. I don't think that decodes to 6 characters (which is what you typed in the password box). - Rocket Hazmat

1 Answers

1
votes

Returning 0 rows is not an "error". It's a valid response to a valid MySQL query. Why is the query returning 0 rows? Because the username and password didn't match.

I assume you are typing 123456 in as your password. No matter how hard you try, when you do base64_encode('123456') you're not gonna get 123456.

What you need to do is remove base64_encode and replace it with a real password solution. PHP 5.5 has one built-in (http://www.php.net/manual/en/ref.password.php)1.

You're gonna need to re-save all your passwords in your database, obviously. Also, make sure to store them all in the same format. You currently have one in plaintext and the other as base64.

1 If you don't have PHP 5.5, you can try this: https://github.com/ircmaxell/password_compat