Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
CREATE TABLE `stopwatch` (`chat_id` int, `timestamp` int);
<?php
$_ENV['BD_NAME'] = '080a06bd157cb3e0b00501b43b43bade';
class Stopwatch
{
private $mysqli;
private $stopwatch_id;
private $db;
function __construct($mysqli, $stopwatch_id) {
$this->db = $_ENV['BD_NAME'];
$this->mysqli = $mysqli;
$this->stopwatch_id = $stopwatch_id;
}
public function start()
{
$timestamp = time();
$query = "
INSERT INTO `$this->db`.`stopwatch` (`chat_id`, `timestamp`)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE timestamp = ?
";
$stmt = $this->mysqli->prepare($query);
/* bind parameters for markers */
$stmt->bind_param("ddd", $this->stopwatch_id, $timestamp, $timestamp);
/* execute query */
return $stmt->execute();
}
public function getTime()
{
$timestamp = time();
$query = "
SELECT `timestamp` FROM `$this->db`.`stopwatch`
WHERE `chat_id` = ?
";
$stmt = $this->mysqli->prepare($query);
/* bind parameters for markers */
$stmt->bind_param("d", $this->stopwatch_id);
/* execute query */
$stmt->execute();
$stmt->bind_result($startTime);
/* fetch values */
$stmt->fetch();
return $startTime;
}
}
$sw = new Stopwatch($mysqli, 1);
$sw->start();
echo $sw->getTime();