oop - PHP user class with login access -


i've searched login system in google thing i'm trying there myself, , not following guide tells me everything.

i have index.php has menu.php included in top, has include login_form.php, calls login.php make login. in login.php uses class user make login. user.php has require_once connection.php

i'm failing @ easy fix can't quite see new me. problem error happens:

undefined variable: mysql in admin\include\lib\user.php on line 12

connection.php:

 require_once 'constants.php';  $mysql = new mysqli($db['host'], $db['username'], $db['password'], $db['name']);  if ($mysql->connect_error){     die('connection error (' . $mysql->connect_errno.')'. $mysql->connect_error); } 

user.php -> line 12:

$this->password=$mysql->real_escape_string($this->password); 

as side note index.php require_once connection.php, constants.php (which has $db array host, user, etc..), , class.php. i'm trying in th oop concept appreciate help!

thanks in advance guys!

edit: believe bit hard give file structure can assure i'm requiring things right. prove did var_dump($mysql). if call in user.php outside class user, shows content. if call inside class user, gives me undefined error.

you have variable scope problem. $mysql out of scope in user class. suggest passing variable class in constructor , store property:

class user {     protected $mysql;         public function __construct($mysql)     {         $this->mysql = $mysql;     }      public function something()     {         $this->password = $this->mysql->real_escape_string($this->password);     } } 

example usage passing connection:

require_once 'connection.php'; // creates $mysql $user = new user($mysql); 

throughout class can use $this->mysql access connection.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -