Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo variable from if statement of other function

Tags:

php

   public function Validate($username,$password) {

   $stmp = $this->db->prepare("SELECT * FROM users WHERE username=? and password = ?");
   $stmp->bindParam(1, $username);
   $stmp->bindParam(2, $password);
   $stmp->execute();

   $this->session = $username;

   if($stmp->rowCount() == 1) {

     $this->session = $username; 

   }

}

public function LoggedIn() {
   echo $this->session;
}

So I have these 2 functions, and I want to be able to echo $this->session inside the LoggedIn(), although it seems impossible right now as the variable is inside an if statement. It works if I put the variable out of the statement. Any suggestions or Idea on how I can do this properly?

like image 214
Alam Avatar asked May 14 '26 09:05

Alam


1 Answers

This is telling you that $stmp->rowCount() is not equal to 1. Do the following:

  1. Run the query manually in your database (eg: using phpMyAdmin). Do you see rows? If not, there's your problem. No rows are matching your query.
  2. If you do get rows back then before your if statement do a var_dump of $stmp->rowCount(). If it returns 0, you want to check the values of $username and $password by var_dumping those. If it returns greater than 1 it means there are duplicate rows in your table. If it returns something else like null, it might be the wrong function to use. In that case check the documentation for the database package you are using.
like image 135
sg- Avatar answered May 15 '26 22:05

sg-