Matt23

DB Manager Class

May 14th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * DbManager
  5.  * Managing database connection
  6.  *
  7.  * @author Matt23
  8.  * @version 1.0
  9.  */
  10. class DbManager
  11. {
  12.     protected $hostname;
  13.     protected $username;
  14.     protected $password;
  15.     protected $database;
  16.  
  17.     protected $connection;
  18.     protected $isConnected;
  19.     protected $error;
  20.  
  21.     public function __construct($hostname, $username, $password, $database)
  22.     {
  23.         $this->hostname = $hostname;
  24.         $this->username = $username;
  25.         $this->password = $password;
  26.         $this->database = $database;
  27.     }
  28.  
  29.     protected function connect()
  30.     {
  31.         try
  32.         {
  33.             $connection = new PDO("mysql:host=$this->hostname;dbname=$this->database", $this->username, $this->password);
  34.             $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  35.             $connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  36.  
  37.             $this->connection = $connection;
  38.             $this->isConnected = true;
  39.         }
  40.         catch (PDOException $e)
  41.         {
  42.             $this->isConnected = false;
  43.             $this->error = $e->getMessage();
  44.         }
  45.     }
  46.  
  47.     protected function getError()
  48.     {
  49.         return $this->error;
  50.     }
  51.  
  52.     public function addVisitor($ip)
  53.     {
  54.         $this->connect();
  55.         if ($this->isConnected)
  56.         {
  57.             try
  58.             {
  59.                 $visitor = $this->connection->prepare('INSERT INTO visitors (ip) VALUES (?)');
  60.                 $visitor->bindParam(1, $ip, PDO::PARAM_STR);
  61.                 $visitor->execute();
  62.                 return array('success' => 'ok');
  63.             }
  64.             catch (PDOException $e)
  65.             {
  66.                 return array('success' => 'no', 'message' => $e->getMessage());
  67.             }
  68.         }
  69.         else
  70.         {
  71.             return array('success' => 'no', 'message' => $this->getError());
  72.         }
  73.     }
  74.  
  75.     public function addEntry($entryId, $voteCount, $maleCount, $femaleCount, $unknownCount, $ip)
  76.     {
  77.         $this->connect();
  78.         if ($this->isConnected)
  79.         {
  80.             try
  81.             {
  82.                 $entry = $this->connection->prepare('INSERT INTO entries (entry_id, votes, men, women, unknown, ip) VALUES (?, ?, ?, ?, ?, ?)');
  83.                 $entry->bindParam(1, $entryId, PDO::PARAM_INT);
  84.                 $entry->bindParam(2, $voteCount, PDO::PARAM_INT);
  85.                 $entry->bindParam(3, $maleCount, PDO::PARAM_INT);
  86.                 $entry->bindParam(4, $femaleCount, PDO::PARAM_INT);
  87.                 $entry->bindParam(5, $unknownCount, PDO::PARAM_INT);
  88.                 $entry->bindParam(6, $ip, PDO::PARAM_STR);
  89.                 $entry->execute();
  90.                 return array('success' => 'ok');
  91.             }
  92.             catch (PDOException $e)
  93.             {
  94.                 return array('success' => 'no', 'message' => $e->getMessage());
  95.             }
  96.         }
  97.         else
  98.         {
  99.             return array('success' => 'no', 'message' => $this->getError());
  100.         }
  101.     }
  102.  
  103.     public function addComment($entryId, $commentId, $voteCount, $maleCount, $femaleCount, $unknownCount, $ip)
  104.     {
  105.         $this->connect();
  106.         if ($this->isConnected)
  107.         {
  108.             try
  109.             {
  110.                 $comment = $this->connection->prepare('INSERT INTO comments (entry_id, comment_id, votes, men, women, unknown, ip) VALUES (?, ?, ?, ?, ?, ?, ?)');
  111.                 $comment->bindParam(1, $entryId, PDO::PARAM_INT);
  112.                 $comment->bindParam(2, $commentId, PDO::PARAM_INT);
  113.                 $comment->bindParam(3, $voteCount, PDO::PARAM_INT);
  114.                 $comment->bindParam(4, $maleCount, PDO::PARAM_INT);
  115.                 $comment->bindParam(5, $femaleCount, PDO::PARAM_INT);
  116.                 $comment->bindParam(6, $unknownCount, PDO::PARAM_INT);
  117.                 $comment->bindParam(7, $ip, PDO::PARAM_STR);
  118.                 $comment->execute();
  119.                 return array('success' => 'ok');
  120.             }
  121.             catch (PDOException $e)
  122.             {
  123.                 return array('success' => 'no', 'message' => $e->getMessage());
  124.             }
  125.         }
  126.         else
  127.         {
  128.             return array('success' => 'no', 'message' => $this->getError());
  129.         }
  130.     }
  131. }
  132.  
  133.  
  134. // Example action
  135. //
  136. //$conn = new DbManager(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
  137. //$conn->addEntry($id[1], $vote_count, $sex['male'], $sex['female'], $sex['unknown'], $_SERVER['REMOTE_ADDR']);
  138. //
Advertisement
Add Comment
Please, Sign In to add comment