php MySQL真正的转义字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4621215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL real escape string
提问by user490895
I was trying to sanitize inputs to my PHP login using addslashes and mysql_real_escape_string. Using addslashes
works, but mysql_real_escape_string
will not.
我试图使用 addslashes 和 mysql_real_escape_string 清理我的 PHP 登录的输入。使用addslashes
作品,但mysql_real_escape_string
不会。
Here's an example of what allows me to log in correctly:
以下是让我正确登录的示例:
$user = addslashes($_POST['user']);<br/>
$password = addslashes($_POST['password']);
And this will not:
这不会:
$user = mysql_real_escape_string($_POST['user']);<br/>
$password = mysql_real_escape_string($_POST['password']);
Also, some of my other fields contain apostrophes. Nothing is returned when using addslashes, since the entry in the DB isn't escaped. I was wondering if using mysql_real_escape_string
could fix this, but I don't know how.
此外,我的其他一些字段包含撇号。使用addslashes 时不会返回任何内容,因为数据库中的条目没有被转义。我想知道使用mysql_real_escape_string
是否可以解决这个问题,但我不知道如何。
回答by Calum
Always use mysql_real_escape_string instead of addslashes. Make sure you are connected to the database beforerunning it otherwise you will error.
始终使用 mysql_real_escape_string 而不是 addslashes。确保在运行之前已连接到数据库,否则会出错。
回答by HADI
mysql_real_escape_string() does not escape % and _. These are wildcards in MySQL if combined with LIKE, GRANT, or REVOKE.
mysql_real_escape_string() 不会转义 % 和 _。如果与 LIKE、GRANT 或 REVOKE 结合使用,则这些是 MySQL 中的通配符。
And Most important thing is don't trust user input.
最重要的是不要相信用户输入。
So, use htmlspecialchars and strip_tags for better security.
因此,使用 htmlspecialchars 和 strip_tags 以获得更好的安全性。
回答by ganesan
Sometimes user may type '
, this symbol but sql treat as an different way and let it not allow to insert in to tables,we cant restrict the user so that we use MySQL real escape string to avoid this kind of error
有时用户可能会输入'
这个符号,但sql将其视为不同的方式,不允许插入到表中,我们不能限制用户,以便我们使用MySQL真正的转义字符串来避免这种错误
Note: if you implement this it should be connect to database else it wont work
注意:如果你实现这个它应该连接到数据库否则它不会工作