Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safely escaping a variable in a raw SQL query

Tags:

sql

php

mysql

xss

Im just wondering, would the following be completely safe or would someone be able to get around it using hexadecimal characters, etc:

$name = mysql_real_escape_string(htmlentities(stripslashes($_REQUEST['name'])));
$query ="SELECT * FROM Games WHERE name LIKE '%{$name}%'";

Thanks.

I know I can use PEAR and other libraries to make prepeared statements. However this question is specifically regaring raw queries.

like image 561
pondpad Avatar asked Mar 15 '26 23:03

pondpad


2 Answers

mysql_real_escape_string does the trick,.,

[EDIT]

for string use:

$str = mysql_real_escape_string($input);

for numeric values type casting is sufficient like:

$val = (int)$input;
like image 86
ovais.tariq Avatar answered Mar 18 '26 13:03

ovais.tariq


Use parametrized prepared-statements, along with PDO, this will give You the most secure way of sending queries, that PHP can offer, and You'll not have to deal with escaping at all, using that way

  • PDO
  • Parametrized prepared-statements
like image 20
canni Avatar answered Mar 18 '26 12:03

canni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!