/ Published in: PHP
Expand |
Embed | Plain Text
class Db { private $link; // Constructor method. // ------------------------------------------------- function Db($host, $username, $password, $database) { $this->connect($host, $username, $password, $database); } function connect($host, $username, $password, $database) { $this->link = mysqli_connect($host,$username,$password,$database); $this->dbprefix = $dbprefix; if (!$this->link) if (!mysqli_set_charset($this->link,'utf8')) } // Delete method. // ------------------------------------------------- function delete($table, $id) { $sql = "DELETE FROM `" . $table . "` WHERE `id` = '" . Db::escape($id) . "'"; return mysqli_query($this->link,$sql); } // Select method. // ------------------------------------------------- function select($table, $id=null) { // No $id given, thus select all rows. $sql = "SELECT * FROM `" . $table . "` ORDER BY `id`"; } // grabs rows matching where clauses given $sql = "SELECT * FROM `" . $table . "` WHERE "; $first=true; foreach($id as $col=>$val) { if($first) $first=false; else $sql.=" AND "; $sql .= "`".Db::escape($col)."` = '" . Db::escape($val) . "'"; } // Grabs the row associated with the given $id. } else { $sql = "SELECT * FROM `" . $table . "` WHERE `id` = '" . Db::escape($id) . "'"; } return mysqli_query($this->link,$sql); } // Update method. // ------------------------------------------------- function update($table, $id) { $getColumns = mysqli_query($this->link,"SELECT * FROM " . $table); while($column = mysqli_fetch_field($getColumns)) { $column = $column->name; Utils::manipulateValues($column); // Manipulate certain values before inserting them into db. // This will be built up-on in the future. } } return mysqli_query($this->link,$sql); } // Insert method. // ------------------------------------------------- function insert($table) { $getColumns = mysqli_query($this->link,"SELECT * FROM " . $table); while($column = mysqli_fetch_field($getColumns)) { $column = $column->name; Utils::manipulateValues($column); // Manipulate certain values before inserting them into db. // This will be built up-on in the future. } } $sql = "INSERT INTO `" . $table . "` (`" . implode("`, `", array_keys($fields)) . "`) VALUES (" . implode(", ", $fields) . ")"; mysqli_query($this->link,$sql); return mysqli_insert_id($this->link); } // Normal query for custom needs. // NOTICE: When using this method, it is your job to assure user submitted-data is secure. // ------------------------------------------------- function query($sql) { return mysqli_query($this->link,$sql); } function num_fields($result) { return mysqli_num_fields($result); } function fetch_field($result) { return mysqli_fetch_field($result); } function fetch_row($result) { return mysqli_fetch_row($result); } function num_rows($result) { return mysqli_num_rows($result); } function fetch_array($result) { return mysqli_fetch_array($result); } function fetch_assoc($result) { return mysqli_fetch_assoc($result); } function escape($string) { return mysqli_real_escape_string($this->link,$string); } // Check for tables existance. function table_exists($sector) { $getTables = mysqli_query($this->link,"SHOW TABLES"); while($table = mysqli_fetch_array($getTables)) { if ($sector == $table[0]) { return true; } } } function show_columns($table, $column) { return mysqli_query($this->link,"SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'"); } } $Db = new Db($settings['database']['databaseHost'], $settings['database']['databaseUsername'], $settings['database']['databasePassword'], $settings['database']['databaseName']);
You need to login to post a comment.
