Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用PDO省略bindParam是安全的吗?_Php_Mysql_Security_Pdo_Sql Injection - Fatal编程技术网

Php 用PDO省略bindParam是安全的吗?

Php 用PDO省略bindParam是安全的吗?,php,mysql,security,pdo,sql-injection,Php,Mysql,Security,Pdo,Sql Injection,我想知道是否必须使用bindParam来防止使用PDO和MYSQL的SQL注入 例如: $username=$_POST['username']; $password=$_POST['password']; $cryptpass=hashFunction($password); $sth=$dbh->prepare("INSERT INTO users(username,password) VALUES(?,?)"); $sth->execute(array($username,$c

我想知道是否必须使用bindParam来防止使用PDO和MYSQL的SQL注入

例如:

$username=$_POST['username'];
$password=$_POST['password'];
$cryptpass=hashFunction($password);
$sth=$dbh->prepare("INSERT INTO users(username,password) VALUES(?,?)");
$sth->execute(array($username,$cryptpass));

写这段代码是安全和正确的方法吗?省略bindParam会缩短代码。

重要的是使用参数,而不是直接替换到查询字符串中。无论是使用
bindParam
还是使用数组参数将参数绑定到
execute
,它们都是等效的。

这仍然是将值绑定到准备好的语句。您正在做的事情与使用
bindParam
函数时一样。所以答案是肯定的,它同样安全
bindParam
只允许比简单地绑定
execute
功能更多的功能,例如:

$sth=$dbh->prepare("Select * from users where status=:v1");
$sth->bindParam(':v1',1,PDO::PARAM_INT);
$sth->execute();

这允许您在默认情况下使用
execute
指定
数据类型
,所有内容都作为字符串发送。此外,您还可以查看此类似问题的答案:

查看此答案以了解详细信息: