Php 条令:绑定变量的数量与令牌的数量不匹配

Php 条令:绑定变量的数量与令牌的数量不匹配,php,symfony,doctrine,query-builder,Php,Symfony,Doctrine,Query Builder,当我执行查询时,它如下所示: SELECT p FROM AppBundle\Entity\Shrubs p WHERE p.botanicalname LIKE :botanicalname AND p.commonname LIKE :commonname AND p.borderlinehardy = :x ORDER BY p.commonname ASC andWhere("p.$key = :x") $shrubs = $query -&

当我执行查询时,它如下所示:

SELECT p FROM AppBundle\Entity\Shrubs p 

WHERE p.botanicalname LIKE :botanicalname 

AND p.commonname LIKE :commonname 

AND p.borderlinehardy = :x

ORDER BY p.commonname ASC 
andWhere("p.$key = :x")
 $shrubs = $query
            ->where($query->expr()->like('p.botanicalname', ':botanicalname'))
            ->setParameter('botanicalname', '%' . $botanicalname . '%')
            ->andwhere($query->expr()->like('p.commonname', ':commonname'))
            ->setParameter('commonname', '%' . $commonname . '%')
            ->orderBy('p.commonname', 'ASC');

        $checkfor = array("wetsoil"=>"Tolerates Wet Soil",
            "borderlinehardy"=>"Borderline Hardy",
            "moistsoil"=>"Prefers Moist Soil",
            "peatysoil"=>"Prefers Peaty Soil",
            "welldrainedsoil"=>"Prefers Well-drained Soil",
            "drought"=>"Tolerates Drought",
            "claysoil"=>"Tolerates Clay Soil",
            "sandysoil"=>"Prefers Sandy Soil",
            "loamsoil"=>"Prefers Loam Soil",
            "infertilesoil"=>"Tolerates Infertile Soil",
            "richsoil"=>"Prefers Rich Soil",
            "compactedsoil"=>"Tolerates Compacted Soil",
            "cityconditions"=>"Tolerates City Conditions",
            "pollution"=>"Tolerates Pollution",
            "salt"=>"Tolerates Salt Conditions",
            "windy"=>"Tolerates Windy Conditions",
            "shade"=>"Prefers Shade",
            "partshade"=>"Prefers Part Shade",
            "fullsun"=>"Prefers Full Sun");

        reset($checkfor);

        foreach ($checkfor as $key => $value) {
            if (${$key} == "x") {
                $shrubs = $query->andWhere("p.$key = ':x'")
                    ->setParameter('x', $key)
                    ->getQuery()
                    ->getResult();
            }
        }
但是我希望x的周围有引号。在查询中,我有这样一行:

SELECT p FROM AppBundle\Entity\Shrubs p 

WHERE p.botanicalname LIKE :botanicalname 

AND p.commonname LIKE :commonname 

AND p.borderlinehardy = :x

ORDER BY p.commonname ASC 
andWhere("p.$key = :x")
 $shrubs = $query
            ->where($query->expr()->like('p.botanicalname', ':botanicalname'))
            ->setParameter('botanicalname', '%' . $botanicalname . '%')
            ->andwhere($query->expr()->like('p.commonname', ':commonname'))
            ->setParameter('commonname', '%' . $commonname . '%')
            ->orderBy('p.commonname', 'ASC');

        $checkfor = array("wetsoil"=>"Tolerates Wet Soil",
            "borderlinehardy"=>"Borderline Hardy",
            "moistsoil"=>"Prefers Moist Soil",
            "peatysoil"=>"Prefers Peaty Soil",
            "welldrainedsoil"=>"Prefers Well-drained Soil",
            "drought"=>"Tolerates Drought",
            "claysoil"=>"Tolerates Clay Soil",
            "sandysoil"=>"Prefers Sandy Soil",
            "loamsoil"=>"Prefers Loam Soil",
            "infertilesoil"=>"Tolerates Infertile Soil",
            "richsoil"=>"Prefers Rich Soil",
            "compactedsoil"=>"Tolerates Compacted Soil",
            "cityconditions"=>"Tolerates City Conditions",
            "pollution"=>"Tolerates Pollution",
            "salt"=>"Tolerates Salt Conditions",
            "windy"=>"Tolerates Windy Conditions",
            "shade"=>"Prefers Shade",
            "partshade"=>"Prefers Part Shade",
            "fullsun"=>"Prefers Full Sun");

        reset($checkfor);

        foreach ($checkfor as $key => $value) {
            if (${$key} == "x") {
                $shrubs = $query->andWhere("p.$key = ':x'")
                    ->setParameter('x', $key)
                    ->getQuery()
                    ->getResult();
            }
        }
但是如果我在x的周围加上单引号,就会得到无效的参数编号错误。如何在x的周围加引号而不出现错误

编辑:我的整个查询如下所示:

SELECT p FROM AppBundle\Entity\Shrubs p 

WHERE p.botanicalname LIKE :botanicalname 

AND p.commonname LIKE :commonname 

AND p.borderlinehardy = :x

ORDER BY p.commonname ASC 
andWhere("p.$key = :x")
 $shrubs = $query
            ->where($query->expr()->like('p.botanicalname', ':botanicalname'))
            ->setParameter('botanicalname', '%' . $botanicalname . '%')
            ->andwhere($query->expr()->like('p.commonname', ':commonname'))
            ->setParameter('commonname', '%' . $commonname . '%')
            ->orderBy('p.commonname', 'ASC');

        $checkfor = array("wetsoil"=>"Tolerates Wet Soil",
            "borderlinehardy"=>"Borderline Hardy",
            "moistsoil"=>"Prefers Moist Soil",
            "peatysoil"=>"Prefers Peaty Soil",
            "welldrainedsoil"=>"Prefers Well-drained Soil",
            "drought"=>"Tolerates Drought",
            "claysoil"=>"Tolerates Clay Soil",
            "sandysoil"=>"Prefers Sandy Soil",
            "loamsoil"=>"Prefers Loam Soil",
            "infertilesoil"=>"Tolerates Infertile Soil",
            "richsoil"=>"Prefers Rich Soil",
            "compactedsoil"=>"Tolerates Compacted Soil",
            "cityconditions"=>"Tolerates City Conditions",
            "pollution"=>"Tolerates Pollution",
            "salt"=>"Tolerates Salt Conditions",
            "windy"=>"Tolerates Windy Conditions",
            "shade"=>"Prefers Shade",
            "partshade"=>"Prefers Part Shade",
            "fullsun"=>"Prefers Full Sun");

        reset($checkfor);

        foreach ($checkfor as $key => $value) {
            if (${$key} == "x") {
                $shrubs = $query->andWhere("p.$key = ':x'")
                    ->setParameter('x', $key)
                    ->getQuery()
                    ->getResult();
            }
        }

为每个调用生成孔查询

$checkfor = array("wetsoil"=>"Tolerates Wet Soil",
            "borderlinehardy"=>"Borderline Hardy",
            "moistsoil"=>"Prefers Moist Soil",
            "peatysoil"=>"Prefers Peaty Soil",
            "welldrainedsoil"=>"Prefers Well-drained Soil",
            "drought"=>"Tolerates Drought",
            "claysoil"=>"Tolerates Clay Soil",
            "sandysoil"=>"Prefers Sandy Soil",
            "loamsoil"=>"Prefers Loam Soil",
            "infertilesoil"=>"Tolerates Infertile Soil",
            "richsoil"=>"Prefers Rich Soil",
            "compactedsoil"=>"Tolerates Compacted Soil",
            "cityconditions"=>"Tolerates City Conditions",
            "pollution"=>"Tolerates Pollution",
            "salt"=>"Tolerates Salt Conditions",
            "windy"=>"Tolerates Windy Conditions",
            "shade"=>"Prefers Shade",
            "partshade"=>"Prefers Part Shade",
            "fullsun"=>"Prefers Full Sun");

reset($checkfor);


foreach ($checkfor as $key => $value) {
      if (${$key} == "x") {
          //create for each call a full query , 
          //maybe you have not $this, then chnage it, 
          //but get a new instacne
          $query = $this->entityManager->createQueryBuilder();
         //create 
         $shrubs = $query
            ->where($query->expr()->like('p.botanicalname', ':botanicalname'))
            ->setParameter('botanicalname', '%' . $botanicalname . '%')
            ->andwhere($query->expr()->like('p.commonname', ':commonname'))
            ->setParameter('commonname', '%' . $commonname . '%')
            ->andWhere("p.$key = :$key")
            ->setParameter($key, $key)
            ->orderBy('p.commonname', 'ASC')   
            ->getQuery()
            ->getResult();
       }
 }

您不能使用占位符两次。使用ANDWHERP.$key=:$key和这个,但我希望x的周围有引号。做不到。因为事先准备好的陈述不是这样的。如果你绑定了参数,你就可以说出它是什么类型,其余的都是在内部完成的。整数类型不获取qouts,但类型字符串将获取qouts。这里的一个大问题是,我们无法知道通过${$key}访问的内容以及这些变量的创建位置。对于无效的参数$query->andWherep.$key=:$key->setParameter$key,$keyquotes,正如我所说的那样,它本身就是api,如果这里使用了真正的准备好的语句,它仍然会给出完全相同的错误!由于某些原因,查询不喜欢这些单引号。它将其丢弃,并且无法正确识别参数。令人难以置信的是,查询的一部分变成了:p.borderlinehardy=:borderlinehardy而不是p.borderlinehardy=:x,所以它周围仍然没有引号。只做->和wherep.$key='$key'不带setParameter怎么样??