. */ require_once(__DIR__ . "/db.php"); class uTrack { public $id; public $userId; public $name; public $comment; public $isValid = false; private static $db; public function __construct($trackId = NULL) { self::$db = uDB::getInstance(); if (!empty($trackId)) { $stmt = self::$db->prepare("SELECT id, user_id, name, comment FROM tracks WHERE id = ? LIMIT 1"); $stmt->bind_param('i', $trackId); $stmt->execute(); $stmt->bind_result($this->id, $this->userId, $this->name, $this->comment); if ($stmt->fetch()) { $this->isValid = true; } $stmt->close(); } } public function add($userId, $name, $comment = NULL) { $trackId = false; if (!empty($userId) && !empty($name)) { $query = "INSERT INTO tracks (user_id, name, comment) VALUES (?, ?, ?)"; $stmt = self::$db->prepare($query); $stmt->bind_param('iss', $userId, $name, $comment); $stmt->execute(); if (!self::$db->error && !$stmt->errno) { $trackId = self::$db->insert_id; } $stmt->close(); } return $trackId; } public function getAll($userId = NULL) { if (!empty($userId)) { $where = "WHERE user_id='" . self::$db->real_escape_string($userId) ."'"; } else { $where = ""; } $query = "SELECT id, user_id, name, comment FROM tracks $where ORDER BY id DESC"; $result = self::$db->query($query); if ($result === false) { return false; } $trackArr = []; while ($row = $result->fetch_assoc()) { $trackArr[] = $this->rowToObject($row); } $result->close(); return $trackArr; } private function rowToObject($row) { $track = new uTrack(); $track->id = $row['id']; $track->userId = $row['user_id']; $track->name = $row['name']; $track->comment = $row['comment']; $track->isValid = true; return $track; } } ?>