SQL Injection Basics — Security

SQL injection happens when user input is joined into the text of a query, so it is read as part of the SQL command. The input ' OR 1=1 -- in SELECT * FROM…

You build a user search box. Type a name, one row comes back, done. A few weeks later, the list of all your app's users is shared on a forum. Nobody stole the database password. They only used that search box, because what was typed into it was read as a command.

This gap is called SQL injection: user input, which should only be data (a name, a number), is read by the database as part of the SQL command. The cause: the input is joined straight into the text of the query. Think of a query as an unfinished sentence: "get the user named ___", where the user's input fills the blank. If someone fills in "Budi, or anyone", the meaning of the sentence changes, and the database runs the new meaning. This is one of the oldest gaps, and still one of the most often found.

How SQL injection works

Joining user input into the text of a query is called concatenation:

// VULNERABLE: input concatenated into the query text
$username = $_GET["username"];
$query = "SELECT * FROM users WHERE username = '$username'";

PHP replaces $username inside the string with its value. So if username holds budi, the query becomes ... WHERE username = 'budi'. That is correct. But username comes from the user, and a user can type anything.

The problem starts when the input contains a quote mark '. That quote closes the string earlier than it should. The database reads the rest of the input after the quote as SQL, not as data.

The OR 1=1 technique: WHERE always true

The most basic technique puts OR 1=1 into the WHERE clause, the part of a query that filters rows. Say the users table has three rows: admin, budi and siti. Before you read on, guess: if the input is ' OR 1=1 --, how many rows come back? The text of the query changes into:

SELECT * FROM users WHERE username = '' OR 1=1 --'

Read that query from left to right:

  1. The quote at the start of the input closes the username string, so the string is empty.
  2. OR 1=1 is added as an extra condition. With OR, one true side is enough. 1=1 is always true, so username = '' OR 1=1 is true for every row.
  3. -- starts an SQL comment. The rest of the original query, the closing quote left hanging, is ignored.

Step 3 holds in SQLite and PostgreSQL. In MySQL and MariaDB, -- must be followed by a space, so --' is not a comment and the query fails with an error. There an attacker writes ' OR 1=1 -- - or ' OR 1=1 #, with the same result.

The result: WHERE matches all rows, so all three users come back: admin, budi, siti.

A query that should fetch one user leaks all of them instead, because the input became part of the command.

Signs and impact

An easy way to spot a vulnerable query: add one ' to the input. If an SQL error appears, your input is very likely being read as part of the query. The impact is real:

Prevention in brief

There is one root cause: user input is joined into the text of the query. There is also one fix: never join input into a query. Use a parameterized query: write the query with a placeholder ? as an empty slot (a parameter), then send the value separately:

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);

This way, ' OR 1=1 -- is searched for as a username exactly as typed (a literal). No user has that name, so 0 rows come back. The next lesson, SQL Injection Prevention, covers how to write safe queries in full.

⚠️ Why is this hole still missed?

TL;DR: SQL injection happens when user input is joined into the text of a query, so it is read as part of the SQL command. The input ' OR 1=1 -- in SELECT * FROM users WHERE username = '<input>' makes WHERE always true, so the whole table (admin, budi, siti) comes back instead of one row. A sign of a vulnerable query: adding one ' causes an SQL error. The impact goes from authentication bypass (logging in without the right password) and data theft (union-based) to changed data. The prevention: don't join input into a query. Use a parameterized query with a ? placeholder (an empty slot for a value), which the next lesson covers in full.

⚡ Input that becomes a command

Write:

$query = "SELECT * FROM users WHERE username = '$username'";

Meaning: PHP puts the value of $username (the user input) straight into the text of the query. If the input contains a quote ', that quote closes the string early, and the database reads the rest as SQL, not data. This gap is called SQL injection.

⚡ OR 1=1

Write:

SELECT * FROM users WHERE username = '' OR 1=1 --'
Guess first: the input is ' OR 1=1 --, and the users table holds admin, budi and siti. How many rows come back?

Three, the whole table. 1=1 is always true, so WHERE matches every row, and -- turns the rest of the query into a comment.

Meaning: A query that should fetch one user leaks all of them instead.

⚡ Signs and impact

Meaning: An easy sign: add one ' to the input. If an SQL error appears, your input is probably being read as part of the query. The impact goes from authentication bypass and data theft through UNION SELECT (union-based) to changed data.

⚡ Don't concatenate, use a placeholder

Write:

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
Guess first: if the input is ' OR 1=1 --, how many rows come back now?

Zero. The value is sent separately, so it is searched for as a username exactly as typed, and no user has that name.

Meaning: Never join input into a query. The ? placeholder (an empty slot in the query) keeps the query apart from the data.

What you will learn