简体   繁体   English

如何知道MySQL何时需要转义

[英]How to know when escape is necessary for MySQL

I'm in the process of building a site with CodeIgniter. 我正在使用CodeIgniter构建一个站点。 This is the 1st site that I've built myself that interacts with a database. 这是我自己构建的第一个与数据库交互的站点。 I'm using MySQL for this project. 我在这个项目中使用MySQL。 How can I tell if data needs to be escaped before saving it to the database? 如何在将数据保存到数据库之前判断数据是否需要转义?

I would advice you to accustom yourself to use prepared statements. 我建议你习惯使用准备好的陈述。 Especially since you are new to working with databases. 特别是因为您不熟悉数据库。 The sooner you start using these, the easier it becomes a second nature. 越早开始使用这些,就越容易成为第二天性。

I, for instance, didn't know about prepared statements when I started with databases. 例如,当我开始使用数据库时,我不知道准备好的语句。 And I experienced my own stubborness when I came in touch with them. 当我与他们联系时,我经历了自己的僵局。 Because I had accustomed myself to another way of doing things already. 因为我已经习惯了另一种做事方式。 Now, this might not be a character trade of yourself, but it doesn't hurt to start as soon as possible with it either way. 现在,这可能不是你自己的角色交易,但无论如何都要尽快开始。

Prepared statements allow you to use placeholders in queries. 准备好的语句允许您在查询中使用占位符。 These placeholders can then be substituted with actual values by binding them to the placeholders. 然后,可以通过将这些占位符绑定到占位符来将这些占位符替换为实际值。 This process of binding, automatically escapes the values. 这个绑定过程会自动转义值。

Here's a (simple) PDO example: 这是一个(简单的) PDO示例:

$db = new PDO( /* some database parameters */ );
$statement = $db->prepare( 'INSERT INTO table VALUES( :username, :password )' );
$statement->bindValue( ':username', $dirtyUsername );
$statement->bindValue( ':password', $dirtyPassword );
$result = $statement->execute();
// result checking ommited for brevity

There's lot's more possibilities with PDO and prepared statements. PDO和准备好的陈述有很多可能性。 For instance you can easily reuse the prepared statement in a loop, as such: 例如,您可以轻松地在循环中重用预准备语句,如下所示:

$statement = $db->prepare( 'INSERT INTO table VALUES( :username, :password )' );
foreach( $users as $dirtyUser )
{
    $statement->bindValue( ':username', $dirtyUser->username );
    $statement->bindValue( ':password', $dirtyUser->password );
    $result = $statement->execute();
    // result checking ommited for brevity
}

Or pass the placeholder bindings to the execute method, like so: 或者将占位符绑定传递给execute方法,如下所示:

$statement = $db->prepare( 'INSERT INTO table VALUES( :username, :password )' );
$result = $statement->execute( array( 
                                   ':username' => $dirtyUsername, 
                                   ':password' => $dirtyPassword
                             ) );
// result checking ommited for brevity

... etc., etc. ......等等

Don't worry about escaping yourself (you WILL screw it up). 不要担心逃避自己(你会搞砸它)。 Use a DB layer where you prepare the statement first, and then add data to it. 使用首先准备语句的数据库层,然后向其中添加数据。

In PHP you should use PDO . 在PHP中,您应该使用PDO You write 你写

SELECT * FROM table WHERE key = :key AND value = :value

and then add the data in by calling functions. 然后通过调用函数添加数据。

If you're using the database class with query bindings , you don't have to do any manual escaping: 如果您正在使用带有查询绑定数据库类 ,则不必执行任何手动转义:

The secondary benefit of using binds is that the values are automatically escaped, producing safer queries. 使用绑定的第二个好处是值会自动转义,从而产生更安全的查询。 You don't have to remember to manually escape data; 您不必记住手动转义数据; the engine does it automatically for you. 引擎会自动为您完成。

If the data is a string, it must always be escaped. 如果数据是字符串,则必须始终对其进行转义。

However, it's better to use parameters instead. 但是,最好使用参数。

When in doubt, escape it all. 如果有疑问,请逃避一切。 Can't be too safe. 不能太安全。

Alright, alright. 好吧好吧。 I get it 我知道了

ALWAYS ESCAPE 总是逃避

If you are generating SQL yourself rather than using something like PDO, then you must always escape strings. 如果您自己生成SQL而不是使用像PDO这样的东西,那么您必须始终转义字符串。

Escaping strings is a basic requirement of the SQL language. 转义字符串是SQL语言的基本要求。 It's what allows you to use characters like apostrophes or backslashes in a string without everything going bad. 它允许你在字符串中使用像撇号或反斜杠这样的字符而不会出现任何问题。 There is no situation at all in which escaping strings is not required. 根本不存在不需要转义字符串的情况。

Even non-strings will have to be filtered to ensure that they are, indeed, non-strings. 甚至非字符串也必须进行过滤,以确保它们确实是非字符串。

If you are learning, please seriously consider learning something like PDO as many others have said, rather than escaping your own strings. 如果你正在学习,请认真考虑像其他许多人所说的那样学习像PDO这样的东西,而不是逃避你自己的字符串。

You escape a MySQL query string when any of the string is made up of user input, for example: 当任何字符串由用户输入组成时,您可以转义MySQL查询字符串,例如:

in PHP: $username = ;//VALUE FROM USER INPUT 在PHP中:$ username =; //来自USER INPUT的值

then your query string is: "INSERT INTO table ('username') VALUES (".$username.")" 那么你的查询字符串是:“INSERT INTO table ('username')VALUES(”。$ username。“)”

You would have to escape this mySQL query due to the fact the $username variable could potentionally have malicous code inserted by the client to be injected into your database. 您必须转义此mySQL查询,因为$ username变量可能会将客户端插入的恶意代码注入您的数据库。

When to escape? 什么时候逃跑? As soon as your site goes public. 一旦您的网站上市。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM