shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

mysqli.php (1578B)


      1 <?php
      2 namespace DB;
      3 final class MySQLi {
      4 	private $connection;
      5 
      6 	public function __construct($hostname, $username, $password, $database, $port = '3306') {
      7 		$this->connection = new \mysqli($hostname, $username, $password, $database, $port);
      8 
      9 		if ($this->connection->connect_error) {
     10 			throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno);
     11 		}
     12 
     13 		$this->connection->set_charset("utf8");
     14 		$this->connection->query("SET SQL_MODE = ''");
     15 	}
     16 
     17 	public function query($sql) {
     18 		$query = $this->connection->query($sql);
     19 
     20 		if (!$this->connection->errno) {
     21 			if ($query instanceof \mysqli_result) {
     22 				$data = array();
     23 
     24 				while ($row = $query->fetch_assoc()) {
     25 					$data[] = $row;
     26 				}
     27 
     28 				$result = new \stdClass();
     29 				$result->num_rows = $query->num_rows;
     30 				$result->row = isset($data[0]) ? $data[0] : array();
     31 				$result->rows = $data;
     32 
     33 				$query->close();
     34 
     35 				return $result;
     36 			} else {
     37 				return true;
     38 			}
     39 		} else {
     40 			throw new \Exception('Error: ' . $this->connection->error  . '<br />Error No: ' . $this->connection->errno . '<br />' . $sql);
     41 		}
     42 	}
     43 
     44 	public function escape($value) {
     45 		return $this->connection->real_escape_string($value);
     46 	}
     47 	
     48 	public function countAffected() {
     49 		return $this->connection->affected_rows;
     50 	}
     51 
     52 	public function getLastId() {
     53 		return $this->connection->insert_id;
     54 	}
     55 	
     56 	public function connected() {
     57 		return $this->connection->ping();
     58 	}
     59 	
     60 	public function __destruct() {
     61 		$this->connection->close();
     62 	}
     63 }