doucang8303 2015-07-22 16:47
浏览 19
已采纳

更新我的PHP API [关闭]

Firstly I've been told that I use outdated MySQL, and should use MySQLi, but I'm not entirely sure how to migrate over.

Also, to further explain this API it shows player stats when you search for their name. For some reason in the background it's querying my url/0 and it's making page loading extremely slow.

Anyhow here's my api.php. Could somebody give me an example of MySQLi?

<?php
// EPICMC CMS API

$date1 = new DateTime('NOW');
$date2 = new DateTime('12/12/2014');

$difference = $date1->diff($date2)->days;

$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database", $link);
if ($_GET['task'] == 'total') {
    $get_db = 'database';
    $result = mysql_query("SELECT * FROM $get_db", $link);

    echo '{"task":"total","amount":"';
    echo mysql_num_rows($result);
    echo '"}';
} elseif ($_GET['task'] == 'info') {
    $get_player = $_GET['player'];
    $get_db     = 'simpleauth_players';
    $result     = mysql_query("SELECT * FROM $get_db WHERE name = '" . mysql_real_escape_string($get_player) . "'", $link);
    while ($data = mysql_fetch_array($result)) {
        echo '{"task":"view","registered":"';
        echo date('m/d/Y h:i A', $data['registerdate']);
        echo '","verified":"';
        echo $data['verified'];
        echo '","banned":"';
        echo $data['banned'];
        echo '","locked":"';
        echo $data['locked'];
        echo '","theme":"';
        echo $data['theme'];
        echo '","email":"';
        echo $data['email'];
        echo '","cover":"';
        echo $data['cover'];
        echo '","ip":"';
        echo $data['lastip'];

        echo '","lastlogin":"';
        echo date('m/d/Y h:i A', $data['logindate']);
        echo '","ip":"';
        echo $data['lastip'];
        echo '"}';
    }
} elseif ($_GET['task'] == 'stats') {
    $get_player = $_GET['player'];
    $get_db     = 'player_stats';
    $result     = mysql_query("SELECT * FROM $get_db WHERE name = '" . mysql_real_escape_string($get_player) . "'", $link);
    while ($data = mysql_fetch_array($result)) {

if($data['skin'] == null){
                                  $skin = $data['name'];
                                  } else {
                                  $skin = $data['skin'];
                                  }

        echo json_encode(array(
            'task' => 'viewstats',
            'skin' => $skin,
            'deaths' => $data['deaths'],
            'kills' => $data['kills'],
            'joins' => $data['joins'],
            'quits' => $data['quits'],
            'kicked' => $data['kicked'],
            'places' => $data['places'],
            'breaks' => $data['breaks'],
            'chats' => $data['chats'],


            // then ratio
            'ratio' => $data['kills'] / $data['deaths']
        ));
    }
} elseif ($_GET['task'] == 'login') {
    $get_user = $_GET['user'];
    $get_db   = 'simpleauth_players';
    $result   = mysql_query("SELECT * FROM $get_db WHERE name = '" . mysql_real_escape_string($get_user) . "'", $link);
    while ($data = mysql_fetch_array($result)) {
        echo '{"task":"login","password":"';
        echo $data['hash'];
        echo '","lastip":"';
        echo $data['lastip'];
        echo '","timestamp":"';
        echo $data['logindate'];
        echo '"}';
    }
} else {
    echo 'online';
}
function hashME($player, $password)
{
    return bin2hex(hash("sha512", $password . strtolower($player), true) ^ hash("whirlpool", strtolower($player) . $password, true));
}
?> 
  • 写回答

1条回答 默认 最新

  • dongzhang6021 2015-07-22 16:51
    关注

    basicaly database comes before queries here is your updated code:

    <?php
    // EPICMC CMS API
    
    $date1 = new DateTime('NOW');
    $date2 = new DateTime('12/12/2014');
    
    $difference = $date1->diff($date2)->days;
    
    $link = mysqli_connect("localhost", "username", "password", "database");
    if ($_GET['task'] == 'total') {
        $get_db = 'database';
        $result = mysqli_query($link, "SELECT * FROM $get_db");
    
        echo '{"task":"total","amount":"';
        echo mysqli_num_rows($result);
        echo '"}';
    } elseif ($_GET['task'] == 'info') {
        $get_player = $_GET['player'];
        $get_db     = 'simpleauth_players';
        $result     = mysqli_query($link, "SELECT * FROM $get_db WHERE name = '" . mysqli_real_escape_string($link, $get_player) . "'");
        while ($data = mysqli_fetch_array($result)) {
            echo '{"task":"view","registered":"';
            echo date('m/d/Y h:i A', $data['registerdate']);
            echo '","verified":"';
            echo $data['verified'];
            echo '","banned":"';
            echo $data['banned'];
            echo '","locked":"';
            echo $data['locked'];
            echo '","theme":"';
            echo $data['theme'];
            echo '","email":"';
            echo $data['email'];
            echo '","cover":"';
            echo $data['cover'];
            echo '","ip":"';
            echo $data['lastip'];
    
            echo '","lastlogin":"';
            echo date('m/d/Y h:i A', $data['logindate']);
            echo '","ip":"';
            echo $data['lastip'];
            echo '"}';
        }
    } elseif ($_GET['task'] == 'stats') {
        $get_player = $_GET['player'];
        $get_db     = 'player_stats';
        $result     = mysqli_query($link, "SELECT * FROM $get_db WHERE name = '" . mysqli_real_escape_string($link, $get_player) . "'");
        while ($data = mysqli_fetch_array($result)) {
    
    if($data['skin'] == null){
                                      $skin = $data['name'];
                                      } else {
                                      $skin = $data['skin'];
                                      }
    
            echo json_encode(array(
                'task' => 'viewstats',
                'skin' => $skin,
                'deaths' => $data['deaths'],
                'kills' => $data['kills'],
                'joins' => $data['joins'],
                'quits' => $data['quits'],
                'kicked' => $data['kicked'],
                'places' => $data['places'],
                'breaks' => $data['breaks'],
                'chats' => $data['chats'],
    
    
                // then ratio
                'ratio' => $data['kills'] / $data['deaths']
            ));
        }
    } elseif ($_GET['task'] == 'login') {
        $get_user = $_GET['user'];
        $get_db   = 'simpleauth_players';
        $result   = mysqli_query($link, "SELECT * FROM $get_db WHERE name = '" . mysqli_real_escape_string($link, $get_user) . "'");
        while ($data = mysqli_fetch_array($result)) {
            echo '{"task":"login","password":"';
            echo $data['hash'];
            echo '","lastip":"';
            echo $data['lastip'];
            echo '","timestamp":"';
            echo $data['logindate'];
            echo '"}';
        }
    } else {
        echo 'online';
    }
    function hashME($player, $password)
    {
        return bin2hex(hash("sha512", $password . strtolower($player), true) ^ hash("whirlpool", strtolower($player) . $password, true));
    }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100