DB.inc 840 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. require_once('config.php');
  3. class DB {
  4. private $connection;
  5. public function select($sql) {
  6. $this->connect();
  7. $result = mysql_query($sql, $this->connection);
  8. if(!$result) {
  9. throw new Exception("DB exception: " . mysql_error($this->connection) . " on query " . $sql);
  10. }
  11. $selectResult = array();
  12. while($row = mysql_fetch_assoc($result)) {
  13. $selectResult[] = $row;
  14. }
  15. return $selectResult;
  16. }
  17. private function connect() {
  18. if(!$this->connection) {
  19. $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS);
  20. if(!$this->connection) {
  21. throw new Exception("DB exception: Unable to connect to the database.");
  22. }
  23. mysql_select_db(DB_NAME, $this->connection);
  24. if(!$this->connection) {
  25. throw new Exception("DB exception: " . mysql_error($connect) . " Could not select database.");
  26. }
  27. }
  28. }
  29. }