我有一个this查询:
$query="select * from news where news_id = (select max(news_id) from news where news_id< $id)";对于execute,我使用class。在这个班级里
public function query($query)
{
$this->_query = filter_var($query, FILTER_SANITIZE_STRING);
$stmt = $this->_prepareQuery();
$stmt->execute();
$results = $this->_dynamicBindResults($stmt);
return $results;
}有没有办法不过滤<信号?
发布于 2013-01-06 08:48:20
不幸的是,整个想法都是错误的。FILTER_SANITIZE_STRING根本帮不上忙。更不用说它只会破坏你的SQL。
要保护SQL不被注入,必须使用预准备语句。因此,不要将变量直接添加到查询中,而是添加一个问号。然后将这个变量放入execute,如下所示
public function query($query, $params)
{
$stmt = $this->mysqli->prepare();
$types = $types ?: str_repeat("s", count($params));
$stmt->bind_param($types, ...$params);
$stmt->execute();
return $stmt->get_result();
}那就这样用吧
$query="select * from news where news_id = (select max(news_id) from news where news_id<?)";
$data = $db->query($query, [$id])->fetch_all(MYSQLI_ASSOC)https://stackoverflow.com/questions/14174054
复制相似问题