What is SQL Injection and how to prevent it with Prepared Statement _
  • January 28, 2025
  • Azad Chouhan
  • 0

SQL injection is a most common security attack that allows the attackers to manipulate the SQL queries by injected the malicious code. To reduce this risk developers must ensure that data should not treated as executable commands. In this article you will learn how to use the prepared statement and parameterized queries as security against SQL injection attacks.

How Important is Prepared Statement?

You can use prepared statement as a powerful tool against SQL Injection. Prepared statement separate the SQL code from the data and make ensure that user input never interpreted as a part of the SQL command. This separation is very important as a security purpose because it makes it impossible to inject harmful SQL code into the query .

During prepared statements , the SQL statement sent to the database server separately from the parameters. With this process the database can parse the SQL command without any risk of malicious input altering database structure

 

How to Implement Prepared Statement?

There are two primary methods to implement prepared statements in PHP: using PDO (PHP Data Objects) and MySQLi (MySQL Improved).

 

There are two ways to implement prepared statements in PHP

  1. Using PDO ( PHP Data Objects)
  2.  MYSQLi (MySQL Improved)

 

Using PDO

PDO provides a consistent interface for accessing various databases. Here’s how to use it:

 

$stmt = $pdo->prepare(‘SELECT * FROM users WHERE name = :name’);
$stmt->execute([‘name’ => $name]);

foreach ($stmt as $row) {
// Process each row
}

In the above code ‘:name‘ is a named parameter and it will be replaced with the actual value of $name  when the statement is executed.

Using MySQLi

$result = $db->execute_query(‘SELECT * FROM users WHERE name = ?’, [$name]);
while ($row = $result->fetch_assoc()) {
// Process each row
}

MySQLi is related to MYSQL database. From PHP 8.2 you can use the execute_query() method which simplifies the process.

 

So now Question is How Prepared Statement Work?

When you prepare a statement , the SQL Command is parsed and compiled by the database server. When you use placeholder either ? or named parameters you are indicating where to insert the actual values. During executing the statement the database combines the complied SQL with the parameters provided and it will ensure that these parameters are treated as a data

Lets Take an example, if the $name variable contain any malicious string like  ‘ Azad’; DELETE FROM users.  the database will search for the string ‘ Azad’; DELETE FROM users without executing the harmful command

 

Here are the some benefits of Prepared Statements

  1. Prepared Statements prevent SQL Injection by treating any input as a data.
  2. It also impact performance, If the same statement is executed multiple times  then its parsed and complied only once to improve efficiency
  3. Prepared Statement simplify the process of executing the SQL Command with variable data.

 

So the conclusion is Prepared statements and parameterized queries are required practice to prevent any type of SQL Injection attack. By separating the SQL command from the user input you can protect applications from malicious attacks and its also improve performance and maintainability.

 

Final Note:

If you come so far thank you for reading my this article. If you find any information which need improvement then you can comment down or email me at co*****@az*********.com.  I will check all the comments and emails and all the relevant  suggestions will be updated in this article.

Leave a Reply

Your email address will not be published. Required fields are marked *