• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

including a PHP variable into SQL query string

ThePiston

Senior member
I need to include a variable inside a string in a SQL query, but something isn't right.

This is the variable:

$curr_yr = date('Y') ;

and I need it to replace the "2015" in the following query:

$query = "SELECT COUNT(`id`)
from j17_cbsubs_subscriptions
WHERE id!=62
and id !=299
and expiry_date < '2015-12-31 23:59:58'
AND status='A'";

any help is appreciated!
 
so this is a bad idea?

$query = "SELECT COUNT(`id`)
from j17_cbsubs_subscriptions
WHERE id!=62
and id !=299
and expiry_date < '{$curr_yr}-12-31 23:59:58'
AND status='A'";
 
so this is a bad idea?

$query = "SELECT COUNT(`id`)
from j17_cbsubs_subscriptions
WHERE id!=62
and id !=299
and expiry_date < '{$curr_yr}-12-31 23:59:58'
AND status='A'";

It depends on where curr_yr is coming from, but in general, yes. Seeing something like that makes me think "SQL injection attack" even if it isn't possible (input guaranteed to be an integer). Parameterized queries aren't much harder to write and they are always safe from injection attacks. It is just a good idea to always prefer them over string concatenation.
 
Back
Top