How do I filter by columns with PDO? I have the following:
$sth = $dbh->query('SELECT * from blog ORDER BY date DESC LIMIT 4');
This obviously returns the last 4 results. I want to add one more step and filter it by a specific column named category
So, something like:
$sth = $dbh->query('SELECT * from blog WHERE category=NAME-HERE ORDER BY date DESC LIMIT 4');
Thanks!
I would suggest using prepared statements with named parameters:
PDO will automatically wrap quotes around the
categoryparameter if it is a string.If the value of
$categoryisn’t coming from an untrusted source, you can modify your code as follows:Note that in the above case, you do need quotes around the
$categoryvariable since you’re building the query string yourself.