php - Using PDO to select when param columns are unknown/variable -
for simplicity's sake, let's have rather contrived table:
[id] [weekday] [weather] 1 sun cloudy 2 mon sunny ... ... ... 8 sun cloudy ... ... ... 15 sun windy
and i'm hitting table datasets. want data based on weekday, based on weather. create class:
class weather { public static function reportbyday($weekday) { return self::weatherserver('weekday',$weekday); } public static function reportbyweather($weather) { return self::weatherserver('weather', $weather) } private static function weatherserver($reporttype, $value) { $q = "select id, weekday, weather table $reporttype = $value"; $r = mysql_query($q); etc etc. return $results; } }
so wanted convert pdo, discovered morning where :field = :thing
structure doesn't work... @ least can't make work.
if delineate column, where weather = :thing
works nicely... i've lost convenience of original class structure because i'd have type out of specialized queries... , there a lot real dataset & table structure.
is there pdo way use params columns? or can params used values?
it looks have half answer -- don't make pdo bind column, "manually" doing:
private static function weatherserver($reporttype, $value) { // may want sanitize reporttype, because private, // might not need $q = "select id, weekday, weather table $reporttype = :value"; // no idea $pdo declared in framework. it's // feel best meets need. $stmt = $pdo->prepare($q); $stmt->bindparam(":value",$value); etc etc. return $results; }
Comments
Post a Comment