skip to Main Content

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;
    }
}
?>

(Edit: This is the current Database.php script)

<?php
error_reporting(E_ERROR | E_PARSE);

        
class Database
{
        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 static 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;
    }
}

?>

This is the Config.php file, which contains the credentials for the SQL connections.

<?php
class Config
{
    const ITEMS_PER_PAGE = '20';
    const RELATIVE_URL = '';

    public $db_data;
    public $db_mulegend_jp;

    public function __construct() {
        $db_data['dbdriver'] = 'sqlsrv';
        $db_data['hostname'] = '192.168.XXX.XXX';
        $db_data['username'] = 'USER';
        $db_data['password'] = 'TEST';
        $db_data['database'] = 'Mu2Auth';
        

        $db_mulegend_jp['hostname'] = 'localhost';
        $db_mulegend_jp['username'] = 'root';
        $db_mulegend_jp['password'] = 'TEST';
        $db_mulegend_jp['database'] = 'mulegend_jp';
        $db_mulegend_jp['dbdriver'] = 'mysqli';

        $this->db_user = $db_user;
        $this->db_mulegend_jp = $db_mulegend_jp;
      }

}

2

Answers


  1. Chosen as BEST ANSWER

    I've checked the phpinfo also, and it looks like all the drivers and extensions are loaded.

    PHPINFO()

    I don't know if something else is wrong in the Database PHP code, but it looks like it is still ignoring the sqlsrv part. ODBC Drivers also have been installed.

    I've even tried to replace the line with the server address to :

    private static $dbHost = '192.168.XXX.XXX, 1443 ' ;

    Error Message

    It looks like somehow, the defined server and credentials are not read from the file.

    <?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 = 'sqlsrv';  //<--- not sure if nessecary
        private static $cont  = null;


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search