I have a function which generates a query. Something like this:
// this is a simplified version of my real code
public function get_query(){
$name = $_GET['name'];
return "SELECT * FROM mytable WHERE name = $name";
}
As you know, such queries aren't safe. Because they are threatened by SQL injection. Anyway, I need to escape $name
variable before using it into the query.
Well there are two approaches: (since I use PDO. Otherwise there is also an old way which is using mysql_escape_string()
)
- Using
PDO::prepare()
- Using
PDO::quote()
Both of them need the PDO connection which isn't access into the generator query function. Now I want to know, is there any other approach?