【发布时间】:2014-07-22 18:42:42
【问题描述】:
我正在开发一个使用 SQL 的项目。现在我创建了一个检查数据库的函数 (check_database($post))。该函数应该能够在任何其他函数中调用。我在同一个类中的另一个函数 (authenticate($post)) 调用此函数。当我尝试 php 文件时,我收到以下错误:
致命错误:在 91 行的 /Applications/XAMPP/xamppfiles/htdocs/Bootstrap-Profile/Code/Controllers/AccountController.php 中调用未定义函数 check_database() >
我的完整代码会明确错误在哪里:
<?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();
}
}
第 91 行:
} else if (check_database($post)) {
我尝试了很多方法,但就是找不到问题所在。希望有人能找到它:)
【问题讨论】:
-
你不能调用类方法,不管是静态的还是其他的,只能通过它们的名字来调用。您可以使用
$this->methodName或self::methodName。在您的情况下,它是self::methodName。 -
@JohnConde 6 年后你救了我的命 :) 用这个明显的注释