php - PDO dynamic queries with prepared statement -


i have pdo class wrapper:

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 => 'set names utf8',                 pdo::attr_persistent => true             );              try {                 $this->dbh = new pdo($dsn, $user, $pass, $options);             }             catch (pdoexception $e) {                 echo $e->getmessage();                 die();             }         }          public function query($query) {             $this->stmt = $this->dbh->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->stmt->bindvalue($pos, $value, $type);              return $this;         }          public function execute($vars = array()) {                 $this->querycounter++;                  if (isset($vars) && count($vars)) {                     foreach ($vars $k => $v ) {                         $this->bind(($k+1), $v);                     }                 }                  return $this->stmt->execute();         }          public function resultset($vars = array()) {             $this->execute($vars);             return $this->stmt->fetchall(pdo::fetch_assoc);         }          public function single($vars = array()) {             $this->execute($vars);             return $this->stmt->fetch();         }     } 

my dynamic query here:

        $per_page           = ($filter["show_by"] >= 25 && $filter["show_by"] <= 100) ? intval($filter["show_by"]) : 25;          $start          = intval($filter["page_id"]) ?  ($filter["page_id"] -1)*$per_page : 0;          $sql_counter    = 'select count(*) count `products` 1=1';         $sql_result = 'select * `products` 1=1';          $data = [];          if (isset($filter["mode"]) != 'extra') {              $sql_counter    .= ' , `status` = :status';             $sql_result     .= ' , `status` = :status';              $data[":status"] = 1;          }          if (intval($filter["category_id"])) {              $sql_counter    .= ' , find_in_set(:category_id, `cid`)';             $sql_result     .= ' , find_in_set(:category_id, `cid`)';              $data[":category_id"] = $filter["category_id"];         }                 if (strlen($filter["search_by"]) > 0) {              $search          = '%'.filter_var($filter["search_by"], filter_sanitize_string).'%';             $sql_counter    .= ' , `name` :search';             $sql_result     .= ' , `name` :search';              $data[":search"] = $search;         }             /*print_r($sql_counter);         print_r($data);         die();         */          $count  = $this->db->query($sql_counter)->single($data)[0]; 

but returns me error:

 pdostatement::execute(): sqlstate[hy093]: invalid parameter number: number of bound variables not match number of tokens 

my debug info below:

select count(*) count `products` 1=1 , `status` = :status , find_in_set(:category_id, `cid`) , `name` :search  array (     [:status] => 1     [:category_id] => 7     [:search] => %123% ) 

question: bug? thanks!

unfortunately, questions "please watch code , tell me bug" sure offtopic on site.

the answer possible tell how debug query , how localize problem.

look, have query , parameters.

try run raw pdo. work?
if not - try take out conditions (along corresponding parameters) 1 one. on 1 stopped? can reproduce problem one?

try narrow problem down as possible. that's way anyway.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -