Omer Aviv
Omer Aviv

Reputation: 284

Database php class causes fatal error

I'm trying to create a php class that will connect me to the database more easily, but it seems to not work.

This is my function from the class:

class Database {
    // host, username, password and db are defined

    public function GetMySqlConnection()
    {
        return mysqli_connect($this->host, $this->username, $this->password, $this->db);
    }
}

And I use it in another page like this:

$db = Database()->GetMySqlConnection();
$result = $db->query("SELECT name FROM users WHERE name='" . mysqli_real_escape_string($_POST['agname']) . "' AND password='" . mysqli_real_escape_string($_POST['agpass']) . "'");

For some reason it causes some error I can't manage to find. What am I doing wrong?

Upvotes: 1

Views: 127

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

Database is class here, so you cannot use $db = Database()->GetMySqlConnection();

You should use:

$db = new Database();
$db = $db->GetMySqlConnection();

instead

Upvotes: 1

Related Questions