/** * Get a value from the database from this specific guess. * * @param string $columnName The column name. * * @return mixed The value. * * @throws Exception Throws if an error occurred. */ private function getDatabaseValue($columnName) { // Prepare a query for the database to list guesses with this ID $statement = Database::getPDO()->prepare('SELECT ' . $columnName . ' FROM ' . GuessManager::getDatabaseTableName() . ' WHERE guess_id=:id'); $statement->bindParam(':id', $this->id, PDO::PARAM_INT); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } // Return the result $data = $statement->fetch(PDO::FETCH_ASSOC); return $data[$columnName]; }
public static function createGuess($firstName, $lastName, $mail, $weight) { // Make sure the name is valid if (!AccountUtils::isValidName($firstName) || !AccountUtils::isValidName($firstName)) { throw new Exception('The name is invalid.'); } // Make sure the mail is valid if (!AccountUtils::isValidMail($mail)) { throw new Exception('The mail is invalid.'); } // TODO: Validate the weight! // Get the session ID $sessionId = getSessionKey(); // Determine the creation date time $dateTime = DateTime::now(); // Get the guess IP $ip = IpUtils::getClientIp(); // Prepare a query for the picture being added $statement = Database::getPDO()->prepare('INSERT INTO ' . static::getDatabaseTableName() . ' (guess_session_id, guess_first_name, guess_last_name, guess_mail, guess_weight, guess_datetime, guess_ip) ' . 'VALUES (:session_id, :first_name, :last_name, :mail, :weight, :guess_datetime, :ip)'); $statement->bindValue(':session_id', $sessionId, PDO::PARAM_STR); $statement->bindValue(':first_name', $firstName, PDO::PARAM_STR); $statement->bindValue(':last_name', $lastName, PDO::PARAM_STR); $statement->bindValue(':mail', $mail, PDO::PARAM_STR); $statement->bindValue(':weight', $weight, PDO::PARAM_STR); // TODO: Use the UTC/GMT timezone! $statement->bindValue(':guess_datetime', $dateTime->toString(), PDO::PARAM_STR); $statement->bindValue(':ip', $ip, PDO::PARAM_STR); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } // Get and return the guess instance return new Guess(Database::getPDO()->lastInsertId()); }
/** * Delete this registry value permanently. * * @throws Exception Throws if an error occurred. */ public function delete() { // Prepare a query for the being deleted $statement = Database::getPDO()->prepare('DELETE FROM ' . Registry::getDatabaseTableName() . ' WHERE registry_id=:id'); $statement->bindValue(':id', $this->getId(), PDO::PARAM_INT); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } }
/** * Check if there's a registry value with a specific key. * * @param string $key The key. * * @return bool True if any registry value exists with this key. * * @throws Exception Throws if an error occurred. */ public static function isValueWithKey($key) { // Trim the key // TODO: Parse the registry key $key = trim($key); // Make sure the key is valid if (strlen($key) <= 0) { throw new Exception('Invalid key.'); } // Prepare a query for the database to list all registry values with the specified key $statement = Database::getPDO()->prepare('SELECT registry_id FROM ' . static::getDatabaseTableName() . ' WHERE registry_key=:registry_key'); $statement->bindValue(':registry_key', $key, PDO::PARAM_STR); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } // Return true if there's any registry found with this ID return $statement->rowCount() > 0; }