I'm sorry about the title being a little unclear but I'm new where. I was wandering around StackOverflow and came across an answer stating that when executing a SQL query, data should never be fed directly ($db->query("SELECT * FROM users WHERE id LIKE $id")
), but should be bound in a prepared statement instead ($db->prepare("SELECT * FROM users WHERE id LIKE ?)->execute(array($id))
).
Now, I'm aware of SQL Injection and that I should use a code that looks like the latter, but my question is; is that always the case? Like, if I had the following code:
$db->query("SELECT * FROM products WHERE id LIKE $id")
Let's suppose that I gave that $id
from within my code, and that it is not an input from the user, would I still have to use a prepared statement? Or would I be fine with the first example?