我写了一个这样的函数:
function tim_kiem($tenchu,$sohieutoba,$sothututhu,$gia_dat){
global $dbh;
$where="1=1";
$tenchu = "%".$tenchu."%";
if($tenchu<>""){
$where=$where." and tenchu like :tenchu";
}
if($sohieutoba<>0){
$where=$where." and (sohieutoba=:sohieutoba)";
}
if($sothututhu<>0){
$where=$where." and (sothututhu=:sothututhu)";
}
if($gia_dat<>""){
$where=$where." and gia_dat=:gia_dat";
}
$sql="SELECT * FROM mybinh WHERE ".$where;
$sth=$dbh->prepare($sql);
$sth->bindValue(':tenchu', $tenchu);
$sth->bindValue(':sohieutoba', $sohieutoba);
$sth->bindValue(':sothututhu', $sothututhu);
$sth->bindValue(':gia_dat', $gia_dat);
$sth->execute();
$row=$sth->fetch(PDO::FETCH_ASSOC);
return $row;
}
结果还可以,但会附加警告
“PDOStatement::bindValue(): SQLSTATE[HY093]: 无效参数号: :sohieutoba ...”
,如果我同时输入$sohieutoba
和$sothututhu
,结果没有任何警告,我不知道我错在哪里。任何建议将不胜感激。