0

Teamspeek、Minecraft などのサーバーのサーバー ステータス (オンライン/オフライン) を確認できるスクリプトを作成しようとしています。

このスクリプトを見つけました:

<?php
    function getStatus($ip,$port){
           $socket = @fsockopen($ip, $port, $errorNo, $errorStr, 3);
           if(!$socket) return "offline";
             else return "online";
        }
    echo getStatus("server.com", "80");
?>

しかし、サーバー名とポートを変更する代わりに、mysql データベースから取得したいと考えています。だから私はこれを作りましたが、私の問題は次のとおりです。スクリプトに接続された文字列を取得できません。文字列を取得するコードは次のようになります。

<?php 
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database  
include "connect_mysql.php"; 
$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
// Use this var to check to see if this ID exists, if yes then get the product 
// details, if no then exit this script and give message why
$sql = mysql_query("SELECT * FROM servers WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    // get all the product details
    while($row = mysql_fetch_array($sql)){ 
        $id = $row["id"];
        $ip = $row["ip"];
        $port = $row["port"];
        $getit = $row["getit"];
        $name = $row["name"];
        $content = $row["content"];
     }

} else {
    echo "That item does not exist.";
    exit();
}       
}
?>

echo getStatus("server.com", "80");だから私はToを変更できると思ったが、echo getStatus("$ip", "$port");それはうまくいかない。私がしなければならないことを知っている人はいますか?

4

1 に答える 1

0

まず最初に。mysql の代わりに mysqli_ を使用します。

次に、結果を 1 に制限している場合は、while ループは必要ありません。

これで、getメソッドでIDを取得すると、データベースで確認して詳細を取得できます。

データベースから詳細を取得したら、関数getStatus()を起動する必要があります。

if ($productCount == 1){
   echo getStatus($row["ip"],$row["port"]);
} else {
         echo "That item does not exist.";
}   
于 2013-10-02T11:44:56.050 に答える