Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用PHP和SQL为我的游戏制作hiscore排行榜_Php_Sql - Fatal编程技术网

尝试使用PHP和SQL为我的游戏制作hiscore排行榜

尝试使用PHP和SQL为我的游戏制作hiscore排行榜,php,sql,Php,Sql,我现在对PHP和SQL非常感兴趣,我正在尝试使用一个旧的PHP示例制作一个在线高分排行榜 链接: 我猜这已经过时了,我试着看看是否可以调试它并检查错误,我确实找到了mysql并尝试用mysqli替换它,但这是一个冒险 有人能帮我做这件事。只有php代码给我带来了问题,我尝试了自托管,并使用了指南中所写的免费主机000webhost 未修改的Config.php: <? /////////////////////////////////////////////////////// // On

我现在对PHP和SQL非常感兴趣,我正在尝试使用一个旧的PHP示例制作一个在线高分排行榜

链接:

我猜这已经过时了,我试着看看是否可以调试它并检查错误,我确实找到了mysql并尝试用mysqli替换它,但这是一个冒险

有人能帮我做这件事。只有php代码给我带来了问题,我尝试了自托管,并使用了指南中所写的免费主机000webhost

未修改的Config.php:

<?
///////////////////////////////////////////////////////
// Online Score Script
// Jeff Vance 
// Version 1.4
//////////////////////////////////////////////////////

// You need to fill in this data from your own mySQL server

// Your host -- for example localhost or mysql.server.com
$mysql_host = "";

// Your user name for mySQL
$mysql_user = "";

// Your password for mySQL
           $mysql_password = "";

// Your database name for mySQL
$mysql_database = "";

// ATTENTION
// This is your secret key - Needs to be the same as the secret key in your game
// You can change this but remember to change it in your game.
// This is used to help secure the score and produce MD5 hashes
           $secret_key = "this is secret";

// Your table name for mySQL
// You can change this is you wish
$tname= 'scores';

// Number of scores to save for each gameid
// Feel free to change this but the example file only lists 10 scores
// You would need to code this
$score_number = '10';

?>

假设您有一个包含两个表的数据库

user
    id
    username
    password

scores
    id
    user_id
    score
    date
…以及用户在播放之前注册/登录到站点。此外,当登录到
user.id
时,它存储在
会话
变量中

最后,假设您使用带有
PDO
mysqli
数据库

<?php

session_start();

$db_host = '127.0.0.1';
$db_user = 'db_username';
$db_pass = 'db_password';
$db_name = 'db_name';

$pdo = new \pdo(
    "mysql:host={$db_host};dbname={$db_name}",
    $db_user,
    $db_pass,
    [
        \PDO::ATTR_ERRMODE          => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_EMULATE_PREPARES => FALSE
    ]
);

$userId = $_SESSION["user_id"] ?? null;
$score  = $_GET["score"]       ?? null;
$date   = date("Y-m-d H:i:s");

if (!$userId) {
    // User isn't logged on
    // 
    exit;
}

if (!is_int($score)) {
    // Not a valid score
    // 
    exit;
}

$sql   = "INSERT INTO scores (`user_id`, `score`, `date`) VALUES (?, ?, ?)";
$query = $pdo->prepare($sql);
$query->execute([$userId, $score, $date]);

user
    id
    username
    password

scores
    id
    user_id
    score
    date
<?php

session_start();

$db_host = '127.0.0.1';
$db_user = 'db_username';
$db_pass = 'db_password';
$db_name = 'db_name';

$pdo = new \pdo(
    "mysql:host={$db_host};dbname={$db_name}",
    $db_user,
    $db_pass,
    [
        \PDO::ATTR_ERRMODE          => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_EMULATE_PREPARES => FALSE
    ]
);

$userId = $_SESSION["user_id"] ?? null;
$score  = $_GET["score"]       ?? null;
$date   = date("Y-m-d H:i:s");

if (!$userId) {
    // User isn't logged on
    // 
    exit;
}

if (!is_int($score)) {
    // Not a valid score
    // 
    exit;
}

$sql   = "INSERT INTO scores (`user_id`, `score`, `date`) VALUES (?, ?, ?)";
$query = $pdo->prepare($sql);
$query->execute([$userId, $score, $date]);