php oop prepare statement
<p>Any idea to avoid to repeat this long messy code every time i want to get the data from the database in secure way?</p>

<pre><code>public function test($param) {
    $sql = "SELECT * FROM users WHERE id = :id AND item_id :item_id";
    $this-&gt;_query = $this-&gt;_db-&gt;pdo()-&gt;prepare($sql);
    $this-&gt;_query-&gt;bindValue(':id', $param);
    $this-&gt;_query-&gt;bindValue(':item_id', $parmas);
    $this-&gt;_query-&gt;execute();
    $this-&gt;_results = $this-&gt;_query-&gt;fetchAll(PDO::FETCH_OBJ);
}
</code></pre>

<p>i created good way not to mess around for a simple one like this</p>

<pre><code>public function query($sql, $params = array()) {
    $this-&gt;_error = false;
    if($this-&gt;_query = $this-&gt;_pdo-&gt;prepare($sql)) {
        if(count($params)) {
            //outside of the loop
            $x = 1;
            foreach($params as $param) {
                $this-&gt;_query-&gt;bindValue($x, $param);
                $x++;
            }
        }
        if($this-&gt;_query-&gt;execute()) {
            $this-&gt;_results = $this-&gt;_query-&gt;fetchAll(PDO::FETCH_OBJ);
            $this-&gt;_count = $this-&gt;_query-&gt;rowCount();
        } else {
            $this-&gt;_error = true;
        }
    } 
    return $this;
}
</code></pre>

<p>pdo is already defined in the construct function in the class.</p>

<p>//im adding more details here and this is what im trying to make.</p>

<pre><code>public function example($table, $params = array(), $extn = array(), $values = array()) {
    $x = '';
    $y = 0;
    foreach($params as $param) {
        $x .= $param;
        if($y &lt; count($params)) {
            $x .= $extn[$y];
        }
        $y++;
    }
    $sql = "SELECT * FROM {$table} WHERE {$x}";
    $this-&gt;_query = $this-&gt;_pdo-&gt;prepare($sql);
    foreach($values as $value) {
        $this-&gt;_query-&gt;bindValue($value);
    }
    $this-&gt;_query-&gt;execute();
    $this-&gt;_results = $this-&gt;_query-&gt;fetchAll(PDO::FETCH_OBJ);
}
</code></pre>

<p>but im getting this error Warning: PDOStatement::bindValue() expects at least 2 parameters,
with this code down below</p>

<pre><code>$test = new Example();
$test-&gt;example('users', array('id = :id', 'username = :username', 'id = :username', 'username = :id'), array(' AND ', ' OR ', ' AND '), array("':id', 4", "':username', 'alex'"));
</code></pre>

<p>any suggestion will be helpful to me!!</p>