无法使用PHP脚本创建新帐户

l3zydbqr  于 2022-12-17  发布在  PHP
关注(0)|答案(2)|浏览(138)

我正在努力与以下网页属于一个游戏服务器创建一个新的帐户在SQL数据库。现在的问题是,由于某种原因,只要我点击注册按钮,什么也没有发生。与开发人员工具,我发现一个错误消息将抛出只要注册已被点击。这是错误消息:致命错误:未捕获错误。无法在(.... pathname)中静态调用非静态方法Database::getData()
此致,
克里斯
我已经尝试了一些网上提到的解决方案。例如,将Database::getData()替换为$Database-〉getData(),但这似乎不起作用,或者我做错了什么。
这是我的register.php文件:

<?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:这是当前的Database.php脚本)

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

?>

这是Config.php文件,其中包含SQL连接的凭据。

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

}
ozxc1zmp

ozxc1zmp1#

在此处包含static关键字

public static function getData() { ... }

然后,您将能够访问它如下:

Database::getData(...);

该方法应该是静态的,因为您正在尝试访问它内部的其他静态方法。

jv4diomz

jv4diomz2#

我也检查了phpinfo,看起来所有的驱动程序和扩展都加载了。
PHPINFO()
我不知道数据库PHP代码中是否有其他错误,但看起来它仍然忽略了sqlsrv部分。ODBC驱动程序也已经安装。
我甚至尝试用服务器地址替换该行,以便:
private static $dbHost = '192.168.XXX.XXX, 1443 ' ;
Error Message
看起来好像是由于某种原因,没有从文件中读取定义的服务器和凭据。

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

相关问题