0

** Following query executes properly and gives exact result for login. But when I trying to access fields returned from the query, it shows me null. In below code, I've checked for correct email id and my column name is 'user_email', it validates the user login correctly, but when I was trying to access the same email id from returned result($checkLogin->user_email), it gives me null **

 $checkLogin=$db->query("SELECT * from `users` where
    `user_email`='$u_email' and `user_password`='$u_password'");
      if($checkLogin)
      {
        $data[]=array(
         'status'=>'success',
         'email'=>$u_email,
         'pass'=>$u_password,
         'email1'=>$checkLogin->user_email
        );
      }

Please give me solution over this

3
  • can you print $checkLogin before if Commented Sep 15, 2017 at 5:35
  • I did it.. Query executes correctly, that's why $checkLogin has been set Commented Sep 15, 2017 at 5:37
  • See about the importance of parametrised queries Commented Sep 15, 2017 at 6:00

2 Answers 2

2

The query() function returns a result object. You need to fetch your result. There are a lot of functions for this: fetch_row(), fetch_assoc(), fetch_array(), fetch_all(). You can find examples of how to use those at php.net.

You could for example write the following:

$checkLogin = $db->query("SELECT * from `users` where `user_email`='$u_email' and `user_password`='$u_password'");

if($row = $checkLogin->fetch_object())
{
    echo $row->user_email;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I found the Solution. I've to use $db->get_row instead of $db->query as follows

  $checkLogin=$db->get_row("SELECT * from `users` where `user_email`='$u_email' and `user_password`='$u_password'");
  if($checkLogin)
  {
    $data[]=array(
     'status'=>'success',
     'email'=>$u_email,
     'pass'=>$u_password,
     'email1'=>$checkLogin->user_email
    );
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.