1

I've searched something about a login system in Google but the thing is that I'm trying to get there by myself, and not following a guide that tells me everything.

I have an index.php that has a menu.php included in the top, which has an include for a login_form.php, which calls login.php to make the login. In the login.php uses the class User to make the login. user.php has the require_once for the connection.php

I'm failing at something very easy to fix but can't quite see it as this is new to me. The problem is that an 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 a side note index.php as a require_once for the connection.php, constants.php (which has the $db array with the host, user, etc..), and the class.php. I'm really trying to get in th OOP concept so I would really appreciate your help!

Thanks in advance guys!

EDIT: I believe that it will be a bit hard to give you the file structure but I can assure you that I'm requiring things right. To prove it I did the var_dump($mysql). If I call it in the user.php but outside the class User, it shows content. However if I call it inside the class User, it gives me the undefined error.

2
  • In user.php, try var_dump()ing $mysql to make sure it is what it should be. Commented Aug 7, 2013 at 13:25
  • Did you forget to include connection.php before you include user.php? Or forget to include it in user.php? (Obviously, I don't know how your files are laid out) Commented Aug 7, 2013 at 13:32

1 Answer 1

3

You have a variable scope problem. $mysql is out of scope in the User class. I suggest passing the variable to the class in the constructor and store it as a 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 the connection:

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

Throughout the class you can now use $this->mysql to access the connection.

Sign up to request clarification or add additional context in comments.

1 Comment

First of all thanks. I actually understood what you said and I was exactly trying to find more on the class scope! I actually did that and then used another method for the login. That way when I construct I only tell it to create the $mysql like you said. Then on the method login I send parameters from the form to make the login. I believe it's that right?

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.