可能重複:
Do I need to escape data to protect against SQL injection when using bind_param() on MySQLi?php mysqli應該使用real_escape_string嗎?
我有一個簡化做一個查詢的類。這裏是一個代碼示例如何插入數據:
$dbi->prepare("INSERT INTO `temp` (`name`,`content`) VALUES (?,?);")->execute($name,$content);
有在課堂上這樣的函數:
public function execute(){
if(is_object($this->connection) && is_object($this->stmt)){
if(count($args = func_get_args()) > 0){
$types = array();
$params = array();
foreach($args as $arg){
$types[] = is_int($arg) ? 'i' : (is_float($arg) ? 'd' : 's');
$params[] = $arg;
/*
or maybe $params[] = $this->connection->real_escape_string($arg);
*/
}
array_unshift($params, implode($types));
call_user_func_array(
array($this->stmt, 'bind_param'),
$this->_pass_by_reference($params)
);
}
if($this->stmt->execute()){
$this->affected_rows = $this->stmt->affected_rows;
return $this;
}
else {
throw new Exception;
}
}
else {
throw new Exception;
}
}
當我宣佈$params[]
我有這樣$params[] = $arg;
我應該把$params[] = $this->connection->real_escape_string($arg);
與否?
謝謝。
不,你不應該。綁定參數分別傳輸到數據庫,因此不需要轉義SQL查詢上下文。 – mario