Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class db {
- protected $db;
- private $db_host = "localhost";
- private $db_user = "root";
- private $db_pass = "root";
- private $db_name = "mvc";
- private $sql;
- private $stmt;
- private $result;
- private $row = array();
- private function __construct() {
- }
- public function connect_error() {
- echo "DB ERROR. If issue persist contact system Admin";
- exit();
- }
- public function error() {
- echo "Error has with the DB";
- exit();
- }
- public function _connect() {
- $this->db = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
- if (mysqli_connect_errno()) {
- $this->connect_error();
- }
- mysqli_set_chartset($this->db, "utf8");
- }
- /**
- * Insert into DB
- */
- public function query($sql, $params = NULL, $sanitize = TRUE) {
- $this->$sql = $this->sanitize($sql, $sanitize);
- if (!$this->stmt = $this->db->prepare($this->sql)) {
- $this->error();
- return false;
- }
- if (is_array($params)) {
- $this->bindparams($params);
- }
- if (!$this->stmt->execute()) {
- $this->error();
- }
- $this->stmt->reset();
- $this->stmt->close();
- }
- public function fetch_val($sql, $bindparams = NULL, $sanitize = TRUE) {
- $this->$sql = $this->sanitize($sql);
- if ($this->sql = $this->db->prepare($this->sql) != TRUE) {
- $this->error();
- }
- if ($this->stmt->execute() != TRUE) {
- $this->error();
- exit();
- } else {
- $this->result = $this->stmt->get_result();
- return $this->result;
- }
- $this->stmt->free_result();
- $this->stmt->close();
- }
- protected function bindparams($arrParams) {
- $params = array();
- $params[0] = "";
- foreach ($arrParams as $key => $value) {
- $params[0] .= $this->gettype($value);
- array_push($params, $arrParams[$key]);
- }
- call_user_func_array(array($this->stmt, 'bind_param'), $params);
- }
- /**
- * returns type of value
- */
- protected function gettype($var) {
- switch (gettype($var)) {
- case 'NULL':
- case 'string':
- return 's';
- break;
- case 'boolean':
- case 'interger':
- return 'i';
- break;
- case 'blob':
- return 'b';
- break;
- case 'double':
- return 'd';
- break;
- }
- }
- /**
- * sanitize the value
- */
- protected function sanitize($sql) {
- $str_sanitized = ($sanitize) ? filter_var($sql, FILTER_SANITIZE_STRING, FITLER_FLAG_NO_ENCODE_QUOTES) : $sql;
- return $str_sanitized;
- }
- /**
- * close db connection
- */
- public function _close() {
- $this->db->close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement