我正在尝试将旧的 MySQLI 查询转换为 PDO,同时还绑定所有变量以防止注入。此特定查询从数组中获取其“WHERE”条件。
$w=array();
if ($pos!='') $w[]="PositionId=':pos'";
if ($country!='') $w[]="CountryId=':country'";
if ($current!='') $w[]="IsCurrent=':current'";
if ($c1stat!='') $w[]=":c1stat :c1comp ':c1val'";
if ($c2stat!='') $w[]=":c2stat :c2comp ':c2val'";
if ($c3stat!='') $w[]=":c3stat :c3comp ':c3val'";
if ($c4stat!='') $w[]=":c4stat :c4comp ':c4val'";
if (count($w)) $where="WHERE ".implode(' AND ',$w); else $where='';
$query="SELECT * FROM table $where";
$result = $pdo->prepare($query);
$result->bindParam(':pos', $pos);
$result->bindParam(':country', $country);
$result->bindParam(':iscurrent', $current);
$result->bindParam(':c1stat', $c1stat); $result->bindParam(':c1comp', $c1comp); $result->bindParam(':c1val', $c1val);
$result->bindParam(':c2stat', $c2stat); $result->bindParam(':c2comp', $c2comp); $result->bindParam(':c2val', $c2val);
$result->bindParam(':c3stat', $c3stat); $result->bindParam(':c3comp', $c3comp); $result->bindParam(':c3val', $c3val);
$result->bindParam(':c4stat', $c4stat); $result->bindParam(':c4comp', $c4comp); $result->bindParam(':c4val', $c4val);
$result->execute();
我仍在尝试了解 PDO 和绑定变量,因此我正在努力进行这一工作。
$query 正在正确构建,例如,生成了带有 $country 条件的 WHERE 语句:
SELECT * FROM skaterRScareer WHERE PositionId>='2' AND CountryId=':country'
那么为什么变量没有绑定呢?提前致谢!