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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -