Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Secure SQL Query in PHP

Tags:

sql

php

mysql

If I am running a query on a MySQL database using PHP as in the following:

$query="SELECT * FROM tablename";

What is the best way to secure this from things like SQL Injections? I've heard about some escape methods, but won't it leave slashes in the query?

like image 787
muttley91 Avatar asked Aug 15 '11 06:08

muttley91


People also ask

How safe PHP files prevent the SQL injection attacks?

PHP has a specially-made function to prevent these attacks. All you need to do is use the mouthful of a function, mysql_real_escape_string . mysql_real_escape_string takes a string that is going to be used in a MySQL query and return the same string with all SQL injection attempts safely escaped.

Which one is secure method in PHP?

PHP Md5 and PHP sha1 Md5 is the acronym for Message Digest 5 and sha1 is the acronym for Secure Hash Algorithm 1. They are both used to encrypt strings. Once a string has been encrypted, it is tedious to decrypt it. Md5 and sha1 are very useful when storing passwords in the database.

Is PHP good for security?

PHP is as secure as any other major language. PHP is as secure as any major server-side language. With the new PHP frameworks and tools introduced over the last few years, it is now easier than ever to manage top-notch security.

What is SQL injection explain how you can prevent it in PHP application?

SQL Injection Prevention in PHPThey force you to define the SQL query and use placeholders for user-provided variables in the query. After the SQL statement is defined, you can pass each parameter to the query. This allows the database to distinguish between the SQL command and data supplied by a user.


1 Answers

The query you have shown in the question doesn't use user supplied values so there is no case of SQL Injection but in a general case:-

Firstly, you must validate all the user input(usernames,emails etc.) before using it in a query. For ex:- If you have allowed only alphanumeric characters in a username, then you must check whether the input is actually alphanumeric or not before proceeding to form a database query and you must also check the size of all the inputs.

After that, in my opinion Prepared Statements is the best choice for preventing SQL injection.

Problem with mysql_real_escape_string():-

As mysql_real_escape_string() escapes characters according to default charset, so it is better than addslashes() function and it properly sanitizes SQL injections arising out of abuse of multibyte character sets, but in another article here, a workaround-scenario is shown that explains that injection can still be done.

Solution:-

So the proper and better way of preventing SQL injection is to use prepared statements. It is a technique in which SQL statements are precompiled before the insertion of the user-input (parameters) and are treated as reusable SQL templates. So, it separates the user input from actual SQL-Code and the SQL-parser never parses the user input.

Apart from security, it also optimizes the SQL query for speed. It helps in cases where you need to run same query multiple times with different user inputs.

You can refer to PHP manual for implementation details.

like image 123
zixtor Avatar answered Nov 09 '22 07:11

zixtor