12

Which means, at the moment, are the safest for screening data in php to send them to the mysql database.

Thank, you )

frops
  • 2,196
  • 4
  • 29
  • 42

3 Answers3

17

I believe mysql_real_escape_string() mysqli_real_escape_string() is the best way to escape input data

Later edit since everything is deprecated now and information must be valid:

Try to use PDO as prepared statements are much safer or mysqli_*() functions if you really need to keep old code somewhat up-to-date.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
5

Currently the most preferred way to insure your safety is prepared statements.

example:

$preparedStatement = $db->prepare('SELECT * FROM memebers WHERE username = :username');

$preparedStatement->execute(array(':username' => $username));

$rows = $preparedStatement->fetchAll();

then when displaying your data use htmlspecialchars()

Songo
  • 5,618
  • 8
  • 58
  • 96
3
validMySQL($var) {
$var=stripslashes($var);
$var=htmlentities($var);
$var=strip_tags($var);
$var=mysql_real_escape_string($var);
return $var
}

The above code helps to sanitize most invalid data, just remember that you've to be connected to mysql database for mysql_real_escape_string to work...

user1260776
  • 316
  • 1
  • 2
  • 8