动态sql查询 - 绑定变量数与令牌数不匹配

时间:2016-08-13 20:36:40

标签: php

我尝试根据获取的信息创建动态查询。

当$ query2为例时:'键入' => ' PvP','在线' => '高级'

$ query是:SELECT * FROM dispserveur WHERE type =:type AND online =:online

这很有效,

$req = $bdd->prepare("$query");
$req->execute(array('type' => 'PvP', 'online' => 'Premium'));

但是当我在执行中使用$ query2变量时,它无效。

$req = $bdd->prepare("$query"); //C
$req->execute(array($query2));

我每次都会得到同样的错误。

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

这是代码,

unset($sql);
unset($sql2);

if (isset($type2) AND $type2 != "all") {
$sql[] = " type = :type ";
$sql2[] = " 'type' => '$type2'";

}

if (isset($online2) AND $online2 != "all") {
$sql[] = " online = :online ";
$sql2[] = " 'online' => '$online2'";

}
if (isset($version2) AND $version2 != "all") {
$sql[] = " version LIKE :version ";
$sql2[] = " 'version' => %$version2%";

}

$query = "SELECT * FROM dispserveur";

if (!empty($sql)) {
$query .= ' WHERE ' . implode(' AND ', $sql);
}
if (!empty($sql2)) {
$query2 = implode(', ', $sql2); 
}

echo $query;
echo "<br />";
echo $query2;

$req = $bdd->prepare("$query"); //C
$req->execute(array($query2));
while ($red = $req->fetch())
{echo "$red[ip]<br />";}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

// Swift 3 class AdaptiveSplitViewController : UISplitViewController { override func viewDidLoad() { super.viewDidLoad() evaulateOverlayType(for: self.traitCollection) } override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { super.willTransition(to: newCollection, with: coordinator) evaulateOverlayType(for: newCollection) } private func evaulateOverlayType(for traits: UITraitCollection) { if traits.horizontalSizeClass == .regular { self.preferredDisplayMode = .primaryOverlay } else { self.preferredDisplayMode = .automatic } } } 的参数应该是一个关联数组,其键与execute()匹配。相反,您使用的是:paramName,它只是一个字符串。这就是你的代码无效的原因。

变化:

$query2

为:

$sql2[] = " 'type' => '$type2'";
$sql2[] = " 'online' => '$online2'";
$sql2[] = " 'version' => %$version2%";

之后,改变

$sql2['type']=$type2;
$sql2['online']=>$online2;
$sql2['version']=>"%$version2%";

$req->execute(array($query2));