如何在 PHP 中使用 mysql_real_escape_string 函数
因此,在我编写的这个程序中,我实际上使用表单从用户那里获取 SQL 查询。然后我继续在我的数据库上运行该查询。
我知道不要“信任”用户输入,所以我想对输入进行清理。我正在尝试使用 mysql_real_escape_string ,但未能成功使其正常工作。
根据输入,这是我正在尝试的: select * from Actor;
//"query" is the input string:
$clean_string = mysql_real_escape_string($query, $db_connection);
$rs = mysql_query($clean_string, $db_connection);
if (!$rs)
{
echo "Invalid input!";
}
这总是给我
“输入无效!”
错误。
当我取出 clean_string
部分并在查询上运行 mysql_query
时,
“无效 输入“
消息不会输出。相反,当我这样做时:
$rs = mysql_query($query, $db_connection);
if (!$rs)
{
echo "Invalid input!";
}
它不会输出
“输入无效”。
但是,我需要使用 mysql_real_escape_string 函数。我做错了什么?
更新:
给定 select * from Actor;
作为输入,我发现了以下内容。
使用 echo 语句我已经 发现在清理之前,字符串保存的值是: 从演员中选择*;
这是正确的。然而,消毒后,它持有不正确的 select *\r\nfrom Actor;
的值,因此出现错误消息。为什么是 mysql_real_escape_string 这样做吗?
So in this program I'm writing, I actually grab a SQL query from the user using a form. I then go on to run that query on my database.
I know not to "trust" user input, so I want to do sanitization on the input. I'm trying to use mysql_real_escape_string
but have been unsuccessful in getting it to work.
Here's what I'm trying, given the input:select * from Actor;
//"query" is the input string:
$clean_string = mysql_real_escape_string($query, $db_connection);
$rs = mysql_query($clean_string, $db_connection);
if (!$rs)
{
echo "Invalid input!";
}
This is ALWAYS giving me the
"Invalid input!"
error.
When I take out the clean_string
part and just run mysql_query
on query, the
"invalid
input"
message is not output. Rather, when I do this:
$rs = mysql_query($query, $db_connection);
if (!$rs)
{
echo "Invalid input!";
}
It does NOT output
"invalid input".
However, I need to use the mysql_real_escape_string
function. What am I doing wrong?
Update:
Givenselect * from Actor;
as an input, I've found the following.
Using echo statements I've
found that before sanitizing, the string holds the value:select * from Actor;
which is correct. However, after sanitizing it holds the incorrect
value of select *\r\nfrom Actor;
, hence the error message. Why ismysql_real_escape_string
doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将其用于查询中的实际值,而不是整个查询字符串本身。
例子:
use it on the actual values in your query, not the whole query string itself.
example:
不要使用过时的 mysql 扩展,而是切换到 PDO。 准备好的语句参数不容易受到注入,因为它们将值与语句分开。准备好的语句和 PDO 还有其他优点,包括性能、易用性和附加功能。如果您需要教程,请尝试“使用 PHP 和 PDO 编写 MySQL 脚本”。
Rather than using the outdated mysql extension, switch to PDO. Prepared statement parameters aren't vulnerable to injection because they keep values separate from statements. Prepared statements and PDO have other advantages, including performance, ease of use and additional features. If you need a tutorial, try "Writing MySQL Scripts with PHP and PDO".
mysql_real_escape_string()
是字符串转义函数。它不会使任何输入安全,只是字符串值,不能与 LIKE 子句一起使用,并且整数仍然需要以不同的方式处理。一个更简单、更通用的示例可能是:
请注意,对于 SQL 字符串,每个变量仍必须用
'
单引号括起来。 (否则转义就没有意义了。)mysql_real_escape_string()
is the string escaping function. It does not make any input safe, just string values, not for use with LIKE clauses, and integers need to be handled differently still.An easier and more universal example might be:
Note how each variable must still be enclosed by
'
single quotes for SQL strings. (Otherwise the escaping would be pointless.)您应该使用 mysql_real_escape_string 转义查询的参数,而不是整个查询本身。
例如,假设您有从表单收到的两个变量。然后,您的代码将如下所示:
You should use
mysql_real_escape_string
to escape the parameters to the query, not the entire query itself.For example, let's say you have two variables you received from a form. Then, your code would look like this:
手册 mysql_real_escape_string()
因此您无法转义整个查询,只能转义数据...因为它将转义所有不安全字符,例如引号(查询的有效部分)。
如果您尝试类似的操作(以转义整个查询)
输出是
并且该查询不再有效。
manual mysql_real_escape_string()
So you can't escape entire query, just data... because it will escape all unsafe characters like quotes (valid parts of query).
If you try something like that (to escape entire query)
Output is
and that is not valid query any more.
这对我有用。德狼 (wtec.co)
This worked for me. dwolf (wtec.co)