绑定变量的数量与sql“IN”的标记数不匹配

时间:2017-04-11 05:59:23

标签: php mysql pdo

这是我的PHP代码:

$roomNo = [1,2,8];

        $inQuery = implode(',', array_fill(0, count($roomNo), '?'));



        $totalRoom = $pdo->prepare('SELECT *
                                        FROM room
                                        WHERE categoryId = ?
                                        AND
                                        id NOT IN (' . $inQuery . ')
                                    ');

        $totalRoom->bindValue(1, $id, PDO::PARAM_INT);
        foreach ($roomNo as $key => $value) {
            $totalRoom->bindValue(($key+1), $value);
        }
        $totalRoom->execute();
        $totalRoom = $totalRoom->fetchAll();

这是错误:

  

未捕获的PDOException:SQLSTATE [HY093]:参数号无效:   绑定变量的数量与令牌的数量不匹配   /家庭/ mohib / MEGA / PHP /核心   项目/酒店客房预订/控制器/预订检查-form.controller.php:66

1 个答案:

答案 0 :(得分:1)

在你的foreach($ roomNo as $ key => $ value)中......第一个$ key == 0。 $ key + 1将重新绑定categoryId。

试试这个:

public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables

Context context;
int[] flag;
LayoutInflater inflater;
int pos;
public ViewPagerAdapter(Context context,  int[] flag) {
    this.context = context;
    this.flag = flag;
}
public ViewPagerAdapter (Context context, int[] flag, int position ){
    this.context = context;
    this.flag = flag;
    this.pos = position;
}

@Override
public int getCount() {
    return flag.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {

    // Declare Variables

    TextView txt;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.viewpager_item, container,
            false);
    // Locate the ImageView in viewpager_item.xml
    txt= (TextView) itemView.findViewById(R.id.flag);
    // Capture position and set to the ImageView
    txt.setText(flag[position]);
    txt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {            
       }
    });
    // Add viewpager_item.xml to ViewPager
    ((ViewPager) container).addView(itemView);

    return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    ((ViewPager) container).removeView((RelativeLayout) object);
 }
}

修改

忘记提及,如果你使用IN()就不需要循环,你可以绑定一个字符串。

  adapter = new ViewPagerAdapter(Image.this, flag );//Here Flag is array
  viewPager.setAdapter(adapter);
  viewPager.setCurrentItem(possition);

为避免混淆索引,您还可以使用命名绑定引用:

$totalRoom->bindValue(($key+2), $value);