php - ping webserver and store in mysql database -
trying ping server using
function pingdomain($domain){ $starttime = microtime(true); $file = fsockopen ($domain, 80, $errno, $errstr, 10); $stoptime = microtime(true); $status = 0; if (!$file) $status = -1; // site down else { fclose($file); $status = ($stoptime - $starttime) * 1000; $status = floor($status); } return $status; }
and i'm trying pass variable off mysql database, record attempts. best way this?
create table in db following columns:
- id - int - auto incrementing
- duration - bigint
- timestamp - timestamp - set now() on insert
then, firstly, connect database:
$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(); }
then insert row so:
$query = $db->prepare("insert the_table_name (duration) values (:duration)"); $query->execute(array(":duration" => $status));
i think should work fine.
Comments
Post a Comment