I maintain an old PHP page that stores sensor readings in MySQL and builds queries like "SELECT * FROM readings WHERE device = '$device'" from a URL parameter. I know this is open to SQL injection and that prepared statements are the recommended fix, but I want to understand why they work rather than just copy a snippet.
How does a prepared statement differ from escaping the input, and are there parts of a query it cannot protect?




