How to escape strings in pdo? [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

This question already has an answer here:

I would like to know how to escape strings in pdo . I have been escaping the springs like in the code bellow but now with pdo I do not know how to do it

$username=(isset($_POST['username']))? trim($_POST['username']): ''; $previlage =(isset($_GET['previlage'])); $query ="SELECT * FROM site_user  WHERE username = '".mysql_real_escape_string($_SESSION['username'])."' AND  previlage ='Admin'"; $security = mysql_query($query)or die (mysql_error($con)); $count = mysql_num_rows($security); 

回答1:

Well, you can use PDO::quote, but, as said in its own docpage...

If you are using this function to build SQL statements, you are strongly recommended to use PDO::prepare() to prepare SQL statements with bound parameters instead of using PDO::quote() to interpolate user input into an SQL statement.

In your case it can look like this:

$query = "SELECT *              FROM site_user             WHERE username = :username AND previlage = 'Admin'"; $sth   = $dbh->prepare($query); $sth->execute(array(':username' => $_SESSION['username']) ); 


回答2:

mysql_* function will not work in PDO. WHY? Because PDO doesnt use mysql to connect to a databases, as far as input sanitization, PDO uses prepared statements you can find a good tutorial for that here: pdo



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!