Stawiam 'pierwsze' kroki w projektowaniu obiektowym - bylbym wdzieczny za opinie i komentarze zwlaszcza krytyczne.
Moim celem jest uproszczenie i ujednolicenie sposobu w jaki komunikuje sie z baza danych.
Czy ktos moze wskazac zagrozenia i luki w bezpieczenstwie - czy cos przegapilem albo poprostu o tym nie wiem ?
Czy mozna dodac cos jeszcze co ulatwiloby polaczenie do DB ?
Dziekuje za poswiecony czas.
Ps. wiem tekze ze moge uzyc $db = new mysqli(); ale chcialem stworzyc cos wlasnego.
(config.php)
<?php ?>
(mysqldatabase.php)
<?php require_once('config.php'); /** * Description of mysqldatabase * @author Pawel Pasich */ class mysqldatabase { private $connection; private $last_query; public $error; public function __construct() { $this->open_connection(); } private function open_connection(){ if (function_exists("mysqli_connect")) { $db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_BASE, DB_PORT); if ($db) { $this->connection = $db; } else { $this->connection = FALSE; $this->error = "Unable to connect to DB: "; if (DEBUG_MODE) { $this->error .= mysqli_connect_error(); $this->send_alert(DEBUG_EMAIL, $this->error); } } } else { if (DEBUG_MODE) { $this->error = "Function mysqli_connect not active"; $this->send_alert(DEBUG_EMAIL, $this->error); } $this->connection = FALSE; } } public function query($sql) { if ($this->connection) { $this->last_query = $sql; if ($result) { return $result; } else { $this->error = "Query error: "; if (DEBUG_MODE) { $this->error .= mysqli_error($this->connection); $this->send_alert(DEBUG_EMAIL, $this->error); } return FALSE; } } else { return FALSE; } } public function fetch_array_assoc($result) { if ($result) { while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } } return $data; } public function fetch_array_object($result) { if ($result) { while($row = mysqli_fetch_object($result)) { $data[] = $row; } } return $data; } public function fetch_array_num($result) { if ($result) { while($row = mysqli_fetch_array($result, MYSQLI_NUM)) { $data[] = $row; } } return $data; } public function num_rows($result) { return mysqli_num_rows($result); } public function insert_id() { return mysqli_insert_id($this->connection); } public function affected_rows() { return mysqli_affected_rows($this->connection); } public function close_connection() { if ($this->connection) { mysqli_close($this->connection); } } private function send_alert($email, $text) { } public function __destruct() { $this->close_connection(); } } ?>