Php 我可以在SQL语句中绑定运算符吗?
致命错误 未捕获异常:“PDO异常” 消息:“SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解第3行“>=”1000”附近要使用的正确语法Php 我可以在SQL语句中绑定运算符吗?,php,mysql,pdo,Php,Mysql,Pdo,致命错误 未捕获异常:“PDO异常” 消息:“SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解第3行“>=”1000”附近要使用的正确语法 没有一个 >= 准备($sql); 如果($operator1!=“无”){ $stmt->bindValue(':operator1',$operator1,PDO::PARAM_STR); $stmt->bindValue(':amount',$amount,PD
没有一个
>=
准备($sql);
如果($operator1!=“无”){
$stmt->bindValue(':operator1',$operator1,PDO::PARAM_STR);
$stmt->bindValue(':amount',$amount,PDO::PARAM_STR);
}
如果($operator2!=“无”){
$stmt->bindValue(':operator2',$operator2,PDO::PARAM_STR);
$stmt->bindValue(':date',$date,PDO::PARAM_STR);
}
如果($operator1!=“无”&&$operator2!=“无”){
$stmt->bindValue(':operator1',$operator1,PDO::PARAM_STR);
$stmt->bindValue(':amount',$amount,PDO::PARAM_STR);
$stmt->bindValue(':operator2',$operator2,PDO::PARAM_STR);
$stmt->bindValue(':date',$date,PDO::PARAM_STR);
}
$stmt->setFetchMode(PDO::FETCH_类,get_称为_类());
$stmt->execute();
返回$stmt->fetchAll();
}
$sql.=“\n其中userBalance:operator1:amount”
您不能绑定运算符,因为DB引擎需要它来创建语句。您遗漏了等号和其他运算符;这是一个打字错误的问题。我将摘要编辑成一个问题的形式。
<select name="operator1" class="form-control mr-sm-2">
<option value="None">None</option>
<option value=">="> >= </option>
<option value="<="> <= </option>
</select>
$user = User::searchUserBuiltQuery($_POST);
var_dump($user);
public static function searchUserBuiltQuery($data)
{
$operator1 = $data['operator1'];
$amount = $data['amount'];
$operator2 = $data['operator2'];
$date = $data['registration_date'];
$sql = 'SELECT * from users
';
$db = static::getDB();
if($operator1 != "None"){
$sql .= "\n WHERE userBalance :operator1 :amount";
}
if($operator2 != "None"){
$sql .= "\n WHERE user_registration_date :operator2 :date";
}
if($operator1 != "None" && $operator2 != "None"){
$sql = "\n WHERE userBalance :operator1 :amount
AND user_registration_date :operator2 :date";
}
$stmt = $db->prepare($sql);
if($operator1 != "None"){
$stmt->bindValue(':operator1', $operator1, PDO::PARAM_STR);
$stmt->bindValue(':amount', $amount, PDO::PARAM_STR);
}
if($operator2 != "None"){
$stmt->bindValue(':operator2', $operator2, PDO::PARAM_STR);
$stmt->bindValue(':date', $date, PDO::PARAM_STR);
}
if($operator1 != "None" && $operator2 != "None"){
$stmt->bindValue(':operator1', $operator1, PDO::PARAM_STR);
$stmt->bindValue(':amount', $amount, PDO::PARAM_STR);
$stmt->bindValue(':operator2', $operator2, PDO::PARAM_STR);
$stmt->bindValue(':date', $date, PDO::PARAM_STR);
}
$stmt->setFetchMode(PDO::FETCH_CLASS, get_called_class());
$stmt->execute();
return $stmt->fetchAll();
}