Sometimes you know something has to be a string, sometimes you know it has to be an integer. Sometimes you know it has to be an email address, sometimes you know it has to be a 4 character long string.
The mysql extension is of course deprecated, and has been for a while. The PHP docs say:
This extension is not recommended for writing new code. Instead,
either the mysqli or PDO_MySQL extension should be used. See also the
MySQL API Overview for further help while choosing a MySQL API.
PDO is the 'right' way to use MYSQL in PHP. If that's not Ok for some reason, use mysqli. But, just use PDO.
From the docs:
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindValue(':calories', $calories, PDO::PARAM_INT);
$sth->bindValue(':colour', $colour, PDO::PARAM_STR);
$sth->execute();
?>
Note that calories is an int, and colour is a string. This is a much much better way to deal with things.
This in conjunction with filter_var
can ensure that things are safe.
http://www.php.net/manual/en/filter.filters.sanitize.php and http://www.php.net/manual/en/filter.filters.validate.php describe the different filters avaliable.