我有以下函数,它允许我对数据库执行查询(在同一类中指定)。
我需要使用bind_param
函数来安全地转义这些变量吗?
如果没有,我如何将传递给函数的变量作为值分配给键?
public function query($dms, $col, $date, $ascDesc)
{
$this->stmt = $this->dbh->prepare("{$dms} {$col} FROM {$this->table} ORDER BY UNIX_TIMESTAMP({$date}) $ascDesc");
$this->stmt->execute();
return $this;
}
最佳答案
这样你就可以使用类似的东西(记住只绑定(bind)值,而不是数组......以防数组使用 foreach 并绑定(bind)每个值):
public function query($dms, $col, $date, $ascDesc)
{
$this->stmt = $this->dbh->prepare("{$dms} :col FROM {$this->table} ORDER BY UNIX_TIMESTAMP(:date) :ascDesc;");
$this->stmt->bindParam(':col', $col, PDO::PARAM_STR);
$this->stmt->bindParam(':date', $date, PDO::PARAM_STR);
$this->stmt->bindParam(':ascDesc', $ascDesc, PDO::PARAM_STR);
$this->stmt->execute();
return $this;
}
关于php - 使用PDO语句时,可以使用函数安全地插入变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893844/