mysql查询获取status.php的数据和数组格式

时间:2016-05-27 18:14:13

标签: php mysql json

索引 这是索引php网站 和索引php包括stats php stats获取索引php $ server = array(" serverip",);格式 ping ping minecraft服务器状态 现在[$ server = array(" serverip",); ] serverip我想读取mysql server_list ip 和支持状态获取服务器IP ping 并返回索引php plaese帮助thankyou

<?php

// Autoloader. Use SPL in a real project.
foreach(array('Server', 'Stats', 'StatsException') as $file) {
include sprintf('%s.php', $file);
}
$db_conn = @mysql_connect("localhost", "svl", "456852123456") or die ("Could not connect: " . mysql_error());
mysql_select_db("ncsv_svl", $db_conn);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $db_conn);
$sql = ("SELECT IP FROM `server_list` ORDER BY `server_list`.`Rank` DESC");
$query = mysql_query($sql);

$servers = array(
"ncsv.tw",
"s.nerd.nu",
"p.nerd.nu",
"hardcore.hcsmp.com",
"theverge.game.nfoservers.com:25565",
);

?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Minecraft Server Status</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="http://www.bootcss.com/p/buttons/css/buttons.css" />
<link href="http://cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<style>tr td,tr th {text-align:center !important}tr td.motd,tr th.motd{text-align:center !important;}</style>
<style>.status{width:50px;}</style>
<style>
body{
            background-image: url('bg.jpg');
            background-repeat: repeat-x;
            background-color: #6B4B34;
        }
        .style1 {color: #00FF00}
.style2 {
color: #FF0000;
font-size: 20px;
}
</style>
<!-- HTML5 shim -->
<!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js</script>
<![endif]-->
</head>
<body>
<div class="container">
    <div class="row" style="margin:15px 0;">
        <h1 class="style1">新世紀麥塊伺服器列表</h1>
        <p class="style1">Copyright © 2016 by Simonchan. All rights reserved.
    <p>     </div>
    <div class="row">
        <br><br><br><br>  <center><a href="#" target="_blank" class="button button-raised button-primary button-pill">登錄伺服器</a>
          <a href="#" target="_blank" class="button button-raised button-pill button-inverse">修改伺服器</a>
          <br><br>
          <span class="style2"><b>列表每10分鐘自動刷新一次</b></span><br>
          <br>
          <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th bgcolor="#FFFFFF" class="status">狀態</th>
                    <th bgcolor="#FFFFFF" class="motd">伺服器名字</th>
                    <th bgcolor="#FFFFFF" class="v">遊戲版本</th>
                    <th bgcolor="#FFFFFF" class="ip">伺服器ip</th>
                    <th bgcolor="#FFFFFF">伺服器簡介</th>
                    <th bgcolor="#FFFFFF">伺服器人數</th>
                    <th bgcolor="#FFFFFF">投票</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($servers as $server): ?>
                <?php $stats = \Minecraft\Stats::retrieve(new \Minecraft\Server($server)); ?>
                <tr>
                    <td bgcolor="#FFFFFF">
                        <?php if($stats->is_online): ?>
                        <span class="badge badge-success"><i class="icon-ok icon-white"></i></span>
                        <?php else: ?>
                        <span class="badge badge-important"><i class="icon-remove icon-white"></i></span>
                        <?php endif; ?>                     </td>
                    <td bgcolor="#FFFFFF" class="motd">
                    <?php
                        $sql = mysql_fetch_assoc(mysql_query("SELECT * FROM `ncsv_svl`.`server_list` where ip = '".$server."'"));
                        echo $sql[motd]
                   ?>
                    </td>
                    <td bgcolor="#FFFFFF" class="v"><code><?php echo $stats->game_version ?></code>
                    <td bgcolor="#FFFFFF" class="ip"><code><?php echo $server; ?></code>
                    <td bgcolor="#FFFFFF">有待開放</td>
                    <td bgcolor="#FFFFFF"><?php printf('%u/%u', $stats->online_players, $stats->max_players); ?></td>
                    <td bgcolor="#FFFFFF"><a href="http://f.ncsv.tw/" target="_blank" class="button button-3d button-action button-circle button-jumbo"><i class="fa fa-thumbs-up"></i></a></td>
                </tr>
                <?php unset($stats); ?>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

status.php

<?php

namespace Minecraft;

class Stats {

public static function retrieve( Server $server ) {

    $socket = stream_socket_client(sprintf('tcp://%s:%u', $server->getHostname(), $server->getPort()), $errno, $errstr, 1);

    if (!$socket) {
        throw new StatsException("Could not connect to the Minecraft server.");
    }

    fwrite($socket, "\xfe\x01");
    $data = fread($socket, 2048); 
    fclose($socket);

    $stats = new \stdClass;

    // Is this a disconnect with the ping?
    if ($data == false AND substr($data, 0, 1) != "\xFF") { 
        $stats->is_online = false;
        return $stats;
    }

    $data = substr($data, 9);
    $data = mb_convert_encoding($data, 'auto', 'UCS-2');
    $data = explode("\x00", $data);

    $stats->is_online = true;
    list($stats->protocol_version, $stats->game_version, $stats->motd, $stats->unkwon, $stats->online_players, $stats->max_players) = $data;

    return $stats;
    var_dump($stats);
}

}

mysql数据

enter image description here

status.php获取Minecraft服务器Ping格式

$servers = array(
"s.nerd.nu",
"p.nerd.nu",
"hardcore.hcsmp.com",
"theverge.game.nfoservers.com:25565",
);

1 个答案:

答案 0 :(得分:0)

while($row = $result->fetch_array()) {
    $data['fieldname1'][] = $row['fieldname1'];
    $data['fieldname2'][] = $row['fieldname2'];
}
$json_var = json_encode($data);

您应该从表中获取所需的详细信息,然后存储在数组变量中,并按上述JSON格式进行编码。