Home > PHP > How can I Prevent SQL Injection in PHP?

How can I Prevent SQL Injection in PHP?

In this article, we will use the best way to do modification into SQL query to prevent SQL injection using PHP.

We will use parameterized queries and prepared statements to protect a database from SQL injection by hackers.

What is SQL injection?

SQL injection in PHP is a common and popular technique or method used by hackers to access the database of your website and can destroy it OR they can also use our information in the wrong way.

When malicious code embedded into SQL query by user input inserted then our website or application becomes vulnerable and the hacker can access our whole database.

So let’s start to become a father of hackers 😉

Use SQL Parameters and Prepared Statements for Protection

To protect a website OR application from SQL injection by attackers, you can use SQL Parameters and Prepared statements.

SQL Parameters are values that are added in SQL query and Prepared statements are an execution method of SQL query.

You have two options to do this.

Using MySqli:

See this code below, how you can use a prepared statement and parameters.

Using PDO:

See this code below, how you can use a prepared statement and parameters.

NOTE: I want to let you know that, when we are using PDO to connect with MySQL database then prepared statement are not allowed by default. You have to enable this by disabling emulation of prepared statements.

Look at the example connection creating with PDO:

Using mysql_real_escape_string():

We can also use mysql_real_escape_string() function to escape characters from string to prevent SQL injection. But if you are using a recent version of PHP (7.*.*) then this function will no longer be available in an entirely new version of PHP.

So for this you can use escape_string(). We can also still use mysql_real_escape_string() function but its only for legacy of PHP.

mysql_real_escape_string() function take a string by prevent SQL injection in PHP and return us to same string and escaped extra quotes(”) from string and return safe SQL query.

Look at the example:

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment