PDO:绑定变量的数量与令牌的数量不匹配(尽管令牌的数量和拼写匹配。)

时间:2016-07-28 20:57:24

标签: php mysql pdo

我的PHP脚本获得了经典PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /var/www/html/sctcc/models/contacts_cat.php on line 124

我一直在调试,查询回应为: SELECT cms_contact.cms_id FROM cms_contact WHERE cms_contact.soft_delete = 0 && (first_name LIKE '%:f_search%' || last_name LIKE '%:l_search%') && cms_cat.cat_id = :category ORDER BY cms_contact.last_name, cms_contact.first_name LIMIT 50 OFFSET 0

参数数组的print_r读作:Array ( [f_search] => Morgan [l_search] => Morgan [category] => 3 )

function get_contacts_search($limit = 50, $offset = 0, $search = null, $search_by = null, $category = null, $show_deleted = 0){
   //Reference: http://stackoverflow.com/questions/10015364/pagination-sql-query-syntax
   //Use MySQL Keywords LIMIT & OFFSET to pageinate a query.
   $sql = 'SELECT
                cms_contact.cms_id
                FROM cms_contact';
  $where = " WHERE cms_contact.soft_delete = $show_deleted";
  $order_by = " ORDER BY cms_contact.last_name, cms_contact.first_name LIMIT $limit OFFSET $offset";
  //$limit_offset = ' LIMIT :limit OFFSET :offset';
  $parameters = array();
  //$parameters['limit'] = $limit;
  //$parameters['offset'] = $offset;
  //$parameters['show_deleted'] = $show_deleted;

  if(isset($search)){
    if($search_by == 'name'){
      $where .= " && (first_name LIKE '%:f_search%' || last_name LIKE '%:l_search%')";
      $parameters['f_search' ] = $search;
      $parameters['l_search'] = $search;
    }elseif($search_by == 'company'){
      $where .= " && company LIKE '%:c_search%'";
      $parameters['c_search' ] = $search;
    }else{
      $where .= " && (first_name LIKE '%:f_search%' || last_name LIKE '%:l_search%')";
      $parameters['f_search' ] = $search;
      $parameters['l_search'] = $search;
    }
  }
  if(isset($category)){
    $where .= " && cms_cat.cat_id = :category";
    $parameters['category'] = $category;
  }
  echo "<div class='greenmessage'>". $sql.$where.$order_by ."</div>";
  $data;
  try{
    $db = connect();
    $query = $db->prepare($sql.$where.$order_by);
    print_r($parameters);
    //echo "<div class='greenmessage'><pre>".print_r($parameters)."</pre></div>";
    $result_set = $query->execute($parameters);

    for($i = 0; $row = $query->fetch(); $i++){
    //echo "CMS ID: {$row['cms_id']}\n";
      $data[$i] = get_contact_categories($row['cms_id']);
      //current(get_contact_with_categories_by_id($row['cms_id']));
      // echo "<pre style='display:block; background-color:#ccffff; border:5px solid blue;'>";
      // print_r(get_contact_with_categories_by_id($row['cms_id']));
      // echo "</pre>";
      // echo "</br>End Contact #{$row['cms_id']}</br>";
    }
    $db = null;
  }catch(PDOException $e){
    log_or_echo($e);
  }
  return $data;
  }

有任何疑问,这里有什么问题?

(如果有人可以建议绑定LIMIT,OFFSET和show_delete的方法,也赞赏 - 似乎它不喜欢我尝试这样做,这就是为什么我们有没有用户控制分页的变量。有关语法的东西。)

0 个答案:

没有答案