I want to eliminate sql injection, should I use mysqli real escape string or is it clear in mysqli? For example
$nick=mysqli_real_escape_string($_POST['nick'])
I want to eliminate sql injection, should I use mysqli real escape string or is it clear in mysqli? For example
$nick=mysqli_real_escape_string($_POST['nick'])
Yes, you can use mysqli_real_escape_string to format strings, if you're going to implement your own query processor, using placeholders or some sort of query builder.
If you're planning to use bare API functions in your code (which is obviously wrong practice but extremely popularized by local folks) - better go for the prepared statements.
Anyway, you can't "eliminate sql injection" using this function alone.
mysql(i)_real_escape_string
functions do not prevent injections and shouldn't be used for whatever protection.You should use prepared statements and pass string data as a parameter but you should not escape it.
This example is taken from the documentation:
Note that the example does not call
mysqli_real_escape_string
. You would only need to usemysqli_real_escape_string
if you were embedding the string directly in the query, but I would advise you to never do this. Always use parameters whenever possible.Related