How SQL injection exploits unsafe queries in PHP - Performance & Efficiency
We want to see how the time to run a PHP query changes when it is unsafe and vulnerable to SQL injection.
How does the way we build queries affect how long they take to run?
Analyze the time complexity of the following code snippet.
$user_input = $_GET['id'];
$query = "SELECT * FROM users WHERE id = " . $user_input;
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'];
}
This code builds a SQL query by directly adding user input, then fetches and prints matching user names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through query results with
mysqli_fetch_assoc. - How many times: Once for each matching row in the database.
The number of rows returned depends on the query built from user input. Unsafe input can cause many rows to be returned.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 (safe id) | 1 row fetched, few operations |
| 10 (malicious input returns 10 rows) | 10 rows fetched, 10 times loop |
| 1000 (malicious input returns 1000 rows) | 1000 rows fetched, 1000 times loop |
Pattern observation: More rows returned means more loop iterations, so execution grows with result size.
Time Complexity: O(n)
This means the time grows linearly with the number of rows returned by the query.
[X] Wrong: "The query time depends only on the input size, not on how the query is built."
[OK] Correct: Unsafe queries can return many more rows than expected, making the loop run longer and increasing execution time.
Understanding how unsafe queries affect execution time helps you write safer code and explain why security matters in real projects.
"What if we used prepared statements instead of direct string building? How would the time complexity change?"
Practice
Solution
Step 1: Understand unsafe SQL queries
Unsafe queries allow user input directly in SQL commands without checks.Step 2: Identify the risk of SQL injection
This lets attackers add harmful commands to steal or change data.Final Answer:
Attackers can insert harmful SQL commands to access or damage the database -> Option CQuick Check:
Unsafe queries = SQL injection risk [OK]
- Thinking unsafe queries improve speed
- Confusing SQL injection with visual changes
- Assuming server blocks all users automatically
Solution
Step 1: Identify direct user input in query string
$query = "SELECT * FROM users WHERE name = '" . $_GET['name'] . "'"; directly inserts $_GET['name'] into the SQL string without protection.Step 2: Compare with prepared statements
Options A, B, and D use prepared statements which safely handle input.Final Answer:
$query = "SELECT * FROM users WHERE name = '" . $_GET['name'] . "'"; -> Option AQuick Check:
Direct input in query = unsafe [OK]
- Confusing prepared statements with unsafe queries
- Thinking all string concatenations are safe
- Ignoring the use of placeholders in queries
?id=1 OR 1=1?
$id = $_GET['id']; $query = "SELECT * FROM users WHERE id = $id"; echo $query;
Solution
Step 1: Substitute the URL parameter into the query
The variable $id becomes the string "1 OR 1=1" directly inserted into the query.Step 2: Form the final SQL query string
The query becomes: SELECT * FROM users WHERE id = 1 OR 1=1, which is a valid SQL statement.Final Answer:
SELECT * FROM users WHERE id = 1 OR 1=1 -> Option BQuick Check:
Unsafe input inserted directly = query with injected condition [OK]
- Assuming input is automatically quoted
- Expecting syntax error without quotes
- Ignoring how SQL interprets OR conditions
$username = $_POST['username']; $query = "SELECT * FROM users WHERE username = '$username'"; $result = mysqli_query($conn, $query);
Solution
Step 1: Check how user input is used in the query
The variable $username is inserted directly into the SQL string without sanitization.Step 2: Suggest the fix to prevent injection
Using prepared statements safely handles input and prevents injection attacks.Final Answer:
Using prepared statements would prevent SQL injection -> Option DQuick Check:
Direct input in query = vulnerability [OK]
- Thinking variable is undefined
- Believing mysqli_query can't run SELECT
- Focusing on missing semicolon instead of security
$email = $_POST['email']; $query = "SELECT * FROM users WHERE email = '$email'"; $result = mysqli_query($conn, $query);Which is the best way to fix it?
Solution
Step 1: Review each option's security
$stmt = $conn->prepare('SELECT * FROM users WHERE email = ?'); $stmt->bind_param('s', $email); $stmt->execute(); uses prepared statements with parameter binding, which safely handles input.Step 2: Compare other options
$email = addslashes($_POST['email']); $query = "SELECT * FROM users WHERE email = '$email'"; mysqli_query($conn, $query); uses addslashes which is not fully safe; C is unsafe direct insertion; D uses htmlspecialchars which is for HTML, not SQL.Final Answer:
$stmt = $conn->prepare('SELECT * FROM users WHERE email = ?'); $stmt->bind_param('s', $email); $stmt->execute(); -> Option AQuick Check:
Prepared statements = best SQL injection protection [OK]
- Relying on addslashes for security
- Using htmlspecialchars for SQL input
- Continuing direct string concatenation
Ready to prove it?
Pass the test and this pattern moves unknown
