viktorino Posted November 19, 2013 Share Posted November 19, 2013 hey guys i`m new in php oop programming and i created a method that selecting data from single table in data base need your reviews and tips here is the code : require_once "Config.Class.php"; require_once "CheckValid.Class.php"; class DBWork { private $dbh; private $config; private $valid; public function __construct(){ $this->config = new Config(); $this->valid = new CheckValid(); try { $this->dbh = new PDO('mysql:host='.$this->config->host.'; dbname='.$this->config->dbName.';charset=utf8', $this->config->userName,$this->config->password); $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $e) { echo $e->getMessage(); } } /* ========= SELECT METHOD ARGUMENTS SETTINGS ================= arg 1 = table name - required. arg 2 = fields - required. for example : array("name","id") very important insert all fields inside array even if you have a single field or you selecting all fields like this (*) olso you can us mysql functions like count() or sum() or any thing alse like this: count(`id`) , sum(`id`). arg 3 = for WHERE clause - optional for exampele : "`id` = " or "`name` = ". arg 4 = for WHERE clause - required if arg 3 was inserted example of use : "`id` = ","3" or "`name` = ","viktor". arg 5 = for AND clause - optional for example : "`custumer` = " or "`age` = ". arg 6 = for AND clause - required if arg 5 was inserted example of use : "`custumer` = ","exists" or "`age` = ","28". arg 7 = ORDER BY clause - optioal for example : "`id`" or "`age`" by default its by "`id`". arg 8 = optional ORDER BY FIRST TO LAST $up = true AND BY LAST TO FIRST $up = false . by default $up = true. arg 9 = LIMIT optional for example "2" or "10". by default there is no limit. EXAMPLE OF USE using all arguments : select("`test`", array("id","name"), "`id` =", "3", "`name`=", "viktor", "`order`" ,$up=true, "2"); using only required arguments : select("`test`", array("id","name")); using order by and limit : select("`test`", array("id","name"), "", "", "", "", "`order`" ,$up=true, "2"); -= VERY IMPORTANT TO LEAVE THE PREVIOUS FIELDS EMPTY IF YOU WANT TO MAKE A REQUEST WITHOUT CLAUSES WHERE and AND =- */ public function select($table, $fields, $where = "", $whereVal="", $and="", $andVal="", $order = "", $up = true, $limit = ""){ for($i = 0; $i < count($fields); $i++){ if(strpos($fields[$i], "(") === false && $fields[$i] != "*"){ $fields[$i] = "`".$fields[$i]."`";} } $fields = implode(",",$fields); if(!$order){ $order = " ORDER BY `id`"; }else{ if($order != " RAND() "){ $order = "ORDER BY $order "; if(!$up) $order .= " DESC "; }else{ $order = " ORDER BY $order "; } } if($limit) $limit = " LIMIT $limit "; if($where){ $query = $this->dbh->prepare("SELECT $fields FROM $table WHERE $where:val $order $limit"); $query->bindParam(":val",$whereVal,PDO::PARAM_STR); }if($and && $where){ $query = $this->dbh->prepare("SELECT $fields FROM $table WHERE $where:val AND $and:val2 $order $limit"); $query->bindParam(":val",$whereVal,PDO::PARAM_STR); $query->bindParam(":val2",$andVal,PDO::PARAM_STR); }if(!$where){ $query = $this->dbh->prepare("SELECT $fields FROM $table $order $limit"); } $query->execute(); $rows = $query->fetchAll(PDO::FETCH_ASSOC); return $rows; $query = null; } } $CMSDBWORK = new DBWork(); $s = $CMSDBWORK->select("`test`",array("name","id")); var_dump($s); echo "<br>"; Link to comment https://forums.phpfreaks.com/topic/284062-vote-development-please/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.