PDO Dynamic query builder I'm trying to improve my php oop programming skills. As an exercise I wrote this class that will generate and execute dynamic sql queries. I've also writed a class to call various sanitization filters that php has built in.

QueryBuilder.php class

&lt;?php

class QueryBuilder{

private $db = null;
private $stmt;
private $table;
private $param;
private $cols, $columns;
private $holders, $placehold;
private $fields, $field;

public $data;
public $results;

public function __construct(\PDO $db){
    $this-&gt;db = $db;
}

public function insert($table, array $data, array $columns){
    $holders = $this-&gt;setHolders($columns);
    $cols = $this-&gt;setColumns($columns);
    $stmt = $this-&gt;db-&gt;prepare("INSERT INTO $table ($cols) VALUES ($holders)");
    return $stmt-&gt;execute($data);
}

public function select($table, array $columns, $field, $param){
    $cols = $this-&gt;setColumns($columns);
    $stmt = $this-&gt;db-&gt;prepare("SELECT $cols FROM $table WHERE $field = ?");
    $stmt-&gt;execute(array($param));
    $result = $stmt-&gt;fetch();
    return json_encode($result);
}

public function edit($table, array $columns, array $data, $param){
    $fields = $this-&gt;setFields($columns);
    $stmt = $this-&gt;db-&gt;prepare("UPDATE $table SET $fields WHERE $param = ?");
    return $stmt-&gt;execute($data);
}

public function delete($table, array $data, $param){
    $stmt = $this-&gt;db-&gt;prepare("DELETE FROM $table WHERE $param = ?");
    return $stmt-&gt;execute($data);
}

private function setColumns(array $columns){
    $cols = implode(', ', array_values($columns));
    return $cols;
}

private function setFields(array $columns){
    $fields = implode(' = ?, ', array_values($columns));
    return $fields.' = ?';
}

private function setHolders(array $columns){
    $holders = array_fill(1 ,count($columns),'?');
    return implode(', ',array_values($holders));
}


}

?&gt;


DataSanitizer.php class

&lt;?php

class DataSanitizer{

private $value;
private $sanitized_value;

public function intSanitize(int $value){
    $sanitized_value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
    return $sanitized_value;
}

public function stringSanitize(string $value){
    $sanitized_value = filter_var($value, FILTER_SANITIZE_STRING);
    return $sanitized_value;
}

public function floatSanitize(float $value){
    $sanitized_value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT);
    return $sanitized_value;
}

public function emailSanitize(string $value){
    $sanitized_value = filter_var($value, FILTER_SANITIZE_EMAIL);
    return $sanitized_value;
}

public function validateEmail(string $value){
  $sanitized_value = filter_var($value, FILTER_VALIDATE_EMAIL);
  return $sanitized_value;
}

}

?&gt;


Classes usage example:

&lt;?php
// Usaually i use the spl_autoloader_register();
require_once 'QueryBuilder.php';
reqiure_once 'DataSanitizer.php';

// this file holds the PDO connection stored inside the $db variable
require_once 'Config.php';

$query = new QueryBuilder($db);
$sanitize = new DataSanitizer;

$table = 'test_table';
$data = array($sanitize-&gt;stringSanitize('hello'),$sanitize-&gt;stringSanitize('world'));
$col = array('col1','col2');

$query-&gt;insert($table, $data, $col);

?&gt;
The attempt is quite good, you managed to elude the most critical faults. There are only rather minor and architectural nitpicks.

First of all, I don't know what does DataSanitizer to do here but it shouldn't be any part of a query builder. Data sanitization is tangential to data persistence and they have nothing in common. So it should be just taken away.

As of the query builder, there are two main problems


a potential vulnerability which I explained recently in the other answer. At the very least all identifiers must be formatted according to database rules. 
select is unusable in the real life. Nobody have seen such a method in the wild, it lurks only in such educational builders. If you want a query builder for SELECT, then either create a real one, with distinct methods for each SQL clause, or stick to a free-form query() method. Trust me, SQL is a precious and powerful programming language, do not diminish it to a crippled gibberish.


Also 


Looks like class variables beside $db are just for decoration, cause they never used in the code? And for the good, so just ditch them away.
consider implementing PSR-4 Autoload for your classes.
usually, the data format for such methods as insert and edit is like 

'col1' =&gt; value1,
'col2' =&gt; value2,


so you have to provide only one array with data. However it has other drawbacks with security already explained above and so must be used with caution


All in all, I believe you would benefit from my article on the common mistakes in database wrappers even if yours is a query builder, not a wrapper.