Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The idea is very simple - the query and the data are sent to the database server <em>separately</em>.<br> That's all.</p> <p>The root of the SQL injection problem is <strong>mixing of the code and the data.</strong> </p> <p>In fact, our SQL query is <strong>a legitimate program</strong>. And we are creating such a program dynamically, by adding some data on the fly. Thus, this data may interfere with the <em>program code</em> and even alter it, as every SQL injection example shows it (all examples in PHP/Mysql):</p> <pre><code>$expected_data = 1; $query = "SELECT * FROM users where id=$expected_data"; </code></pre> <p>will produce a regular query </p> <pre><code>SELECT * FROM users where id=1 </code></pre> <p>while this code</p> <pre><code>$spoiled_data = "1; DROP TABLE users;" $query = "SELECT * FROM users where id=$spoiled_data"; </code></pre> <p>will produce a malicious sequence</p> <pre><code>SELECT * FROM users where id=1; DROP TABLE users; </code></pre> <p>It works because we are adding the data directly to the program body and it become a part of the program, so the data may alter the program and depending on the data passed, we will have either a regular output or a table <code>users</code> deleted.</p> <p>While <strong>in case of prepared statements we don't alter our program, it remains intact</strong><br> That's the point. </p> <p>We are sending a <em>program</em> to the server first</p> <pre><code>$db-&gt;prepare("SELECT * FROM users where id=?"); </code></pre> <p>where the data is substituted by some <em>variable</em> called a parameter or a placeholder.</p> <p>Note that the very same query being sent to the server, without any data in it! And then we're sending the data with the <em>second</em> request, essentially separated from the query itself:</p> <pre><code>$db-&gt;execute($data); </code></pre> <p>so, it can't alter our program and do any harm.<br> Quite simple - isn't it? </p> <p>However, it worth to be noted that <strong>not every time</strong> you're using a placeholder, <strong>it is processed as a prepared statement</strong>.</p> <p>A placeholder is a general idea for substituting the actual data with a variable for the future processing (see <code>printf()</code> for example), while a prepared statement is the only a subset of it. </p> <p>There are cases (notably PDO in PHP can do it) when a prepared statement can be emulated, and a query is actually composed along with data and sent to the server in one request. But it is important to understand that this approach is <strong>equally safe</strong>, because <strong>every bit of data is properly formatted according to it's type</strong> and therefore nothing wrong could be happen.</p> <p>The only thing I have to add that always omitted in the every manual:</p> <p>Prepared statements can protect only <em>data</em>, but <strong>can't defend the program itself</strong>.<br> So, once we have to add, say, a dynamical <strong>identifier</strong> - a field name, for example, prepared statements can't help us. I've <a href="https://stackoverflow.com/q/8255054/285587"><strong>explained the matter recently</strong></a>, so I won't repeat myself. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload