sql注入

pdo如何防范sql注入

小新
199
2021-02-05 14:01:21
栏目: 云计算

pdo如何防范sql注入

pdo防范sql注入的方法:

在对应的文件中添加以下代码:

$dbh = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass');

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

代码使用实例:

$dbh = new PDO("mysql:host=localhost; dbname=demo", "user", "pass");

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //禁用prepared statements的仿真效果

$dbh->exec("set names 'utf8'");

$sql="select * from test where name = ? and password = ?";

$stmt = $dbh->prepare($sql);

$exeres = $stmt->execute(array($testname, $pass));

if ($exeres) {

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

print_r($row);

}

}

$dbh = null;

0
看了该问题的人还看了