Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * DbManager
- * Managing database connection
- *
- * @author Matt23
- * @version 1.0
- */
- class DbManager
- {
- protected $hostname;
- protected $username;
- protected $password;
- protected $database;
- protected $connection;
- protected $isConnected;
- protected $error;
- public function __construct($hostname, $username, $password, $database)
- {
- $this->hostname = $hostname;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- }
- protected function connect()
- {
- try
- {
- $connection = new PDO("mysql:host=$this->hostname;dbname=$this->database", $this->username, $this->password);
- $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- $this->connection = $connection;
- $this->isConnected = true;
- }
- catch (PDOException $e)
- {
- $this->isConnected = false;
- $this->error = $e->getMessage();
- }
- }
- protected function getError()
- {
- return $this->error;
- }
- public function addVisitor($ip)
- {
- $this->connect();
- if ($this->isConnected)
- {
- try
- {
- $visitor = $this->connection->prepare('INSERT INTO visitors (ip) VALUES (?)');
- $visitor->bindParam(1, $ip, PDO::PARAM_STR);
- $visitor->execute();
- return array('success' => 'ok');
- }
- catch (PDOException $e)
- {
- return array('success' => 'no', 'message' => $e->getMessage());
- }
- }
- else
- {
- return array('success' => 'no', 'message' => $this->getError());
- }
- }
- public function addEntry($entryId, $voteCount, $maleCount, $femaleCount, $unknownCount, $ip)
- {
- $this->connect();
- if ($this->isConnected)
- {
- try
- {
- $entry = $this->connection->prepare('INSERT INTO entries (entry_id, votes, men, women, unknown, ip) VALUES (?, ?, ?, ?, ?, ?)');
- $entry->bindParam(1, $entryId, PDO::PARAM_INT);
- $entry->bindParam(2, $voteCount, PDO::PARAM_INT);
- $entry->bindParam(3, $maleCount, PDO::PARAM_INT);
- $entry->bindParam(4, $femaleCount, PDO::PARAM_INT);
- $entry->bindParam(5, $unknownCount, PDO::PARAM_INT);
- $entry->bindParam(6, $ip, PDO::PARAM_STR);
- $entry->execute();
- return array('success' => 'ok');
- }
- catch (PDOException $e)
- {
- return array('success' => 'no', 'message' => $e->getMessage());
- }
- }
- else
- {
- return array('success' => 'no', 'message' => $this->getError());
- }
- }
- public function addComment($entryId, $commentId, $voteCount, $maleCount, $femaleCount, $unknownCount, $ip)
- {
- $this->connect();
- if ($this->isConnected)
- {
- try
- {
- $comment = $this->connection->prepare('INSERT INTO comments (entry_id, comment_id, votes, men, women, unknown, ip) VALUES (?, ?, ?, ?, ?, ?, ?)');
- $comment->bindParam(1, $entryId, PDO::PARAM_INT);
- $comment->bindParam(2, $commentId, PDO::PARAM_INT);
- $comment->bindParam(3, $voteCount, PDO::PARAM_INT);
- $comment->bindParam(4, $maleCount, PDO::PARAM_INT);
- $comment->bindParam(5, $femaleCount, PDO::PARAM_INT);
- $comment->bindParam(6, $unknownCount, PDO::PARAM_INT);
- $comment->bindParam(7, $ip, PDO::PARAM_STR);
- $comment->execute();
- return array('success' => 'ok');
- }
- catch (PDOException $e)
- {
- return array('success' => 'no', 'message' => $e->getMessage());
- }
- }
- else
- {
- return array('success' => 'no', 'message' => $this->getError());
- }
- }
- }
- // Example action
- //
- //$conn = new DbManager(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
- //$conn->addEntry($id[1], $vote_count, $sex['male'], $sex['female'], $sex['unknown'], $_SERVER['REMOTE_ADDR']);
- //
Advertisement
Add Comment
Please, Sign In to add comment