2015-06-04 66 views
0

嘗試ping使用平web服務器和存儲在MySQL數據庫

function pingDomain($domain){ 
    $starttime = microtime(true); 
    $file  = fsockopen ($domain, 80, $errno, $errstr, 10); 
    $stoptime = microtime(true); 
    $status = 0; 

if (!$file) $status = -1; // Site is down 
else { 
    fclose($file); 
    $status = ($stoptime - $starttime) * 1000; 
    $status = floor($status); 
} 
return $status; 
} 

服務器現在我試圖傳遞的變量關閉MySQL數據庫,記錄嘗試。什麼是最好的方法來做到這一點?

+0

你問最好的方式來存儲從MySQL的值在MySQL數據庫/表? – Eric

回答

0

在你的數據庫具有以下列創建一個表:

  • ID - INT - 自動遞增
  • 時間 - BIGINT
  • 時間戳 - - 上插入
設置爲NOW()

然後,首先連接到數據庫:

$server = 'localhost'; 
$username = 'your_username_here'; 
$password = 'your_password_here'; 
$database = 'your_db'; 
try 
{ 
    $db = new PDO("mysql:host=$server;dbname=$database;charset=UTF8", $username , $password); 
} 

catch (PDOException $e) 
{ 
    print "Error!: " . $e->getMessage() . "<br/>"; 
    die(); 
} 

然後插入該行像這樣:

$query = $db->prepare("INSERT INTO the_table_name (duration) VALUES (:duration)"); 
$query->execute(Array(":duration" => $status)); 

我想,這應該只是罰款。

相關問題