I'm struggling with the following webpage which belongs to a Gameserver to create a new account within the SQL Database. Now the problem is, for some reason, as soon as I click the register button, nothing happens. With the Developer tool I've found out an error message will be thrown as soon as Register has been clicked. This is the error message : Fatal error: uncaught Error. Non static method Database::getData() cannot be called statically in (.... pathname)
Kind regards,
Chris
I Already tried some sollutions mentioned on the internet. For example to replace the Database::getData() to $Databse->getData() but this does not seem to work, or i'm doing something wrong.
This is my register.php file:
<?php
require 'vendor/autoload.php';
include_once('config/database.php');
include_once('config/config.php');
include_once('config/helper.php');
$config = new Config();
$request_params = getRequests();
$submitted_username = '';
$alert_message = '';
$relative_path = '';
if (!empty($_POST)) {
$username = $_POST['username'];
// Get user by username
$db_user = $config->db_user;
$sql = "SELECT * FROM dbo.TblAccount WHERE accountid = '$username'";
$user_data = Database::getData($db_user, $sql, 'row');
//$user_data = $dbName->getData($db_user, $sql, 'row'); <--- this I've tested.
// Check password
if ($user_data) {
echo 0;
die;
}else{
$hash_password = md5($_POST['password']);
$db_user = $config->db_user;
$sql = "EXEC dbo.regnewaccount '$username','$hash_password'";
$user_data = Database::getData($db_user, $sql, 'row');
echo 1;
die;
}
}
?>
And here is my Database.php file (removed credentials ;-) )
<?php
error_reporting(E_ERROR | E_PARSE);
class Database
{
private static $dbName = 'Mu2Auth' ;
private static $dbHost = 'localhost' ;
private static $dbUsername = 'XX';
private static $dbUserPassword = 'XXXXXXXXXX';
private static $dbdriver = 'MSSQL'; //<--- not sure if nessecary
private static $cont = null;
public function __construct() {
exit('Init function is not allowed');
}
public static function connect($db_data) {
$dbName = $db_data['database'];
$dbHost = $db_data['hostname'];
$dbUsername = $db_data['username'];
$dbUserPassword = $db_data['password'];
$dbDriver = $db_data['dbdriver'];
// One connection through whole application
if ( null == self::$cont ) {
try {
switch($dbDriver){
case 'sqlsrv':
self::$cont = new PDO( "sqlsrv:server=".$dbHost."; Database=".$dbName.";", $dbUsername, $dbUserPassword);
///$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
break;
default:
self::$cont = new PDO( "mysql:host=".$dbHost.";"."dbname=".$dbName. ";charset=utf8", $dbUsername, $dbUserPassword);
break;
}
} catch(PDOException $e) {
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect() {
self::$cont = null;
}
public function getData($db_data, $sql,$type="list"){
self::connect($db_data);
switch($type){
case 'row':
$raw_data = self::$cont->query($sql)->fetch(PDO::FETCH_ASSOC);
break;
default:
$raw_data = self::$cont->query($sql)->fetchAll(PDO::FETCH_ASSOC);
break;
}
self::disconnect();
return $raw_data;
}
public function updateData($db_data, $sql){
self::connect($db_data);
$raw_data = self::$cont->exec($sql);
self::disconnect();
return $raw_data;
}
}
?>
CodePudding user response:
Include static
keyword here
public static function getData() { ... }
Then you will be able to access it like:
Database::getData(...);
The method should be static because you are trying to access other static methods inside it.