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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制