1

I'm working on a project that uses SQL. Now I have made a function that checks the database (check_database($post)). That function should be able to be called in any other function. My other function (authenticate($post)), in the same class, calls this function. When I try the php file out, I get the following error:

Fatal error: Call to undefined function check_database() in /Applications/XAMPP/xamppfiles/htdocs/Bootstrap-Profile/Code/Controllers/AccountController.php on line 91

My full code will make clear where the error is:

<?php
require_once('ConnectionController.php'); // requires
require_once('LayoutController.php');

class AccountController {
    private $connection;

    function __construct(){
        $server = new ConnectionController();
        $this->connection = $server->getConnection();
    }

    /**
     * Get account.
     * @return array|bool
     */
    function getUser($email){
        $query = "SELECT Naam, Email, Password, AccountStatus FROM Gebruiker WHERE Email=".$email.";";

        if($result = $this->connection->query($query)){
            return $result->fetch_assoc();
        }else{
            return false;
        }
    }

    /**
     * Create random string
     * @param int $length
     * @return string
     */
    function generateRandomString($length) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

    static function check_database($post) {
        $query = "SELECT Password, AccountStatus FROM Gebruiker WHERE Email='" .trim($post['email']). "'";

        if($result = $this->connection->query($query)){

            if (password_verify($post['password'], $result['Password'])) {

                if($result['AccountStatus'] == 'Actief') {
                    $_SESSION['email']=trim($post['email']);

                    return true;

                } else {
                    alert("warning", "<b>Foutmelding! </b>Je account is nog niet geactiveerd! Check je mail.");

                    return false;
                }

            } else {
                alert("danger", "<b>Foutmelding! </b>Het e-mailadres en wachtwoord komen niet overheen!");   


                return false;
            }

        } else {

            alert("danger", "<b>Foutmelding! </b>Het opgegeven e-mailadres bestaat niet!");

            return false;
        }
    }

    /**
     * Login the user in.
     * @param $account
     * @return array|bool

     * $email = trim($account['inputLoginEmail']);
     */
    function authenticate($post){
        if (!isset($post['email']) || empty($post['email'])) {
            alert("danger", "<b>Foutmelding! </b>Vul een e-mailadres in!");

        } else if (!filter_var(trim($post['email']), FILTER_VALIDATE_EMAIL)) {
            alert("danger", "<b>Foutmelding! </b>Ongeldig e-mailadres!");

        } else if (!isset($post['password']) || empty($post['password'])) {
            alert("danger", "<b>Foutmelding! </b>Vul een wachtwoord in!");

        } else if (check_database($post)) {
            loadpage('profile.php');
        }
    }

    function __destruct(){
        $this->connection->close();
    }
}

Line 91:

} else if (check_database($post)) {

I tried many things, but I just can't find the issue. Hopefully someone can find it :)

2
  • 4
    You don't call class methods, static or otherwise, only by their name. You use either $this->methodName or self::methodName. In your case it is self::methodName. Commented Jul 22, 2014 at 18:43
  • @JohnConde after 6 years you saved my live :) with this obvious note Commented May 28, 2020 at 22:10

2 Answers 2

1

Call your member function differently. You seem to know that you use $this for "normal" methods (as shown in __destruct(). For static members, you refer to the class name:

if (AccountController::check_database($post)) {
   ...
}

or you use self when referring to methods in the same class:

if (self::check_database($post)) {
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have defined check_database function static. Because of this you have to call it with self.

Like so:

self::check_database($post)

1 Comment

Unfortunately this didn't worked. In fact, it gave a new error: Fatal error: Using $this when not in object context in /Applications/XAMPP/xamppfiles/htdocs/Bootstrap-Profile/Code/Controllers/AccountController.php on line 44. Line 44 is if($result = $this->connection->query($query)){

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.