PHP OOP, undefined property. How can I get this to work? -


okay i'm trying improve php oop, i'm not sure how should this. please point out issue?

the code returns following errors:

notice: undefined property: registration::$userexist fatal error: call member function fetch() on non-object

the class:

class registration extends connect{          public function userexist(){             global $dsn;          $userexist = $this->dsn->prepare("             select * accounts             username= :username             ");  $userexist->bindparam(':username', $username);             $userexist->execute();          $rows = $this->userexist->fetch(pdo::fetch_num);             return $rows;         } 

how i'm trying use class on page:

$conn = new connect(); $registration  = new registration();  $rows = $registration->userexist();                     if($rows < 1){ // 

the error says cannit find variable $userexist, because on last line looking variable "$userexist" inside "$this" while defined normal variables few lines above it.

to fix problem, drop this-> part of line gives error pick variable local scope

$rows = $userexist->fetch(pdo::fetch_num); 

edit said in comments variable $username defined before called method userexists(). however, in php variables won't copied on inside of function code block when called. pass variables need add argument function.

public function userexist($username){         ...         $rows = $userexist->fetch(pdo::fetch_num);         ....     } 

and need use like:

.... $rows = $registration->userexist($username); .... 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -