Note that there are some explanatory texts on larger screens.

plurals
  1. POPDO dynamic queries with prepared statement
    primarykey
    data
    text
    <p>I have a PDO class wrapper:</p> <pre><code>class DB { private $dbh; private $stmt; private $queryCounter = 0; public function __construct($user, $pass, $dbname) { $dsn = 'mysql:host=localhost;dbname=' . $dbname; $options = array( PDO::MYSQL_ATTR_INIT_COMMAND =&gt; 'SET NAMES utf8', PDO::ATTR_PERSISTENT =&gt; true ); try { $this-&gt;dbh = new PDO($dsn, $user, $pass, $options); } catch (PDOException $e) { echo $e-&gt;getMessage(); die(); } } public function query($query) { $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query); return $this; } public function bind($pos, $value, $type = null) { if( is_null($type) ) { switch( true ) { case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; break; default: $type = PDO::PARAM_STR; } } $this-&gt;stmt-&gt;bindValue($pos, $value, $type); return $this; } public function execute($vars = array()) { $this-&gt;queryCounter++; if (isset($vars) &amp;&amp; count($vars)) { foreach ($vars as $k =&gt; $v ) { $this-&gt;bind(($k+1), $v); } } return $this-&gt;stmt-&gt;execute(); } public function resultset($vars = array()) { $this-&gt;execute($vars); return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_ASSOC); } public function single($vars = array()) { $this-&gt;execute($vars); return $this-&gt;stmt-&gt;fetch(); } } </code></pre> <p>My dynamic query is here:</p> <pre><code> $per_page = ($filter["show_by"] &gt;= 25 &amp;&amp; $filter["show_by"] &lt;= 100) ? intval($filter["show_by"]) : 25; $start = intval($filter["page_id"]) ? ($filter["page_id"] -1)*$per_page : 0; $sql_counter = 'SELECT COUNT(*) count FROM `products` WHERE 1=1'; $sql_result = 'SELECT * FROM `products` WHERE 1=1'; $data = []; if (isset($filter["mode"]) != 'extra') { $sql_counter .= ' AND `status` = :status'; $sql_result .= ' AND `status` = :status'; $data[":status"] = 1; } if (intval($filter["category_id"])) { $sql_counter .= ' AND FIND_IN_SET(:category_id, `cid`)'; $sql_result .= ' AND FIND_IN_SET(:category_id, `cid`)'; $data[":category_id"] = $filter["category_id"]; } if (strlen($filter["search_by"]) &gt; 0) { $search = '%'.filter_var($filter["search_by"], FILTER_SANITIZE_STRING).'%'; $sql_counter .= ' AND `name` LIKE :search'; $sql_result .= ' AND `name` LIKE :search'; $data[":search"] = $search; } /*print_r($sql_counter); print_r($data); die(); */ $count = $this-&gt;db-&gt;query($sql_counter)-&gt;single($data)[0]; </code></pre> <p>But this returns me an error: </p> <pre><code> PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens </code></pre> <p>My debug info is below:</p> <pre><code>SELECT COUNT(*) count FROM `products` WHERE 1=1 AND `status` = :status AND FIND_IN_SET(:category_id, `cid`) AND `name` LIKE :search Array ( [:status] =&gt; 1 [:category_id] =&gt; 7 [:search] =&gt; %123% ) </code></pre> <p>Question: Where is a bug? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload