以下是从与变量$fromCity
、匹配的表中查找地址的查询
$fromCity= "324 West Gore Street, Orlando, FL 32806, USA";
$sql = "SELECT * FROM vendor_info WHERE mailing_address LIKE '$fromCity'";
$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$company = $stmt->fetchAll();
dd($company);
在表vendor_info
中,列mailing address
类似于324 WEST GORE STREETORLANDO, FL 32806
如果mailing_address
列中有任何一个单词匹配,我想得到结果
现在dd(company);
的结果是空数组,
请帮我找出这个解决方案,它在symfony,中
可能是这样的:
$fromCity="324 West Gore Street,Orlando,FL 32806";$fromCity_arr=爆炸(',',$fromCity(;$whereStr='';foreach($fromCity_arr为$fromCity _el({if(!empty($where Str(({$where Str.="AND";}$whereStr.="mailing_address LIKE'%"$来自City_el。"%'";}$sql="SELECT*FROM vendor_info WHERE$whereStr";……
结果:
选择*来自供应商信息其中mailing_address LIKE'%324 West Gore Street%'以及mailing_address LIKE'%Orlando%'以及mailing_address LIKE'%FL 32806%'
@mscdeveloper的答案基本上是正确的,但当你使用条令时,你也应该使用准备好的语句来避免sql注入。
$fromCity = "324 West Gore Street, Orlando, FL 32806";
$fromCity_arr = explode(', ', $fromCity);
$stm = $this->getEntityManager()->getRepository(MyModel::class)->getQueryBuilder('a');
for($i=0;$i<count($fromCity_arr);$i++) {
$stm->andWhere('a.mailing_address like ?)
->setParameter($i+1, '%'.$fromCity_arr[$i].'%');
}
$result = $stm->getQuery()->getResult();
dump($stm->getQuery()->getSql());
dd($result);