2013-10-02 67 views
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"); 
?> 

,而不必改變我wnat從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");echo getStatus("$ip", "$port");但是,這並不工作。 有沒有人知道我必須做什麼?

+0

'$ ip'和'$ port'的值是什麼? –

回答

0

首先要做的事情。使用mysqli_而不是mysql。其次,如果你將結果限制爲1,那麼你不需要while循環。

現在,當你得到ID得到方法,您可以檢查它的數據庫並獲取詳細信息:

你將不得不解僱功能的getStatus()一旦你從細節數據庫。

if ($productCount == 1){ 
    echo getStatus($row["ip"],$row["port"]); 
} else { 
     echo "That item does not exist."; 
} 
+1

謝謝。我會說如何工作:) –