Jump to content

vote development please


viktorino

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.