php - Problems with PDO bindParam/Value -
i trying query database, however, value have bindparam
(:name)
not being bound, when echo $sql
, print_r($stmttwo)
where
clause states where :name
instead of string $wherefinal
.
the code have is:
$sql= "select species.species_id species join ( select species.species_id, count(*) mynum species_opt left join species on (species.species_id = species_opt.so_species_id) :name group so_species_id having mynum = 6 ) mytable on species.species_id = mytable.species_id"; $stmttwo = $pdo->prepare($sql); $stmttwo->bindparam(':name', $wherefinal); $stmttwo->execute();
with $wherefinal
being defined before sql statement , being defined as:
$where = ""; foreach ($_post $k => $v){ $where .= "(species_opt.so_option_id = $v) or "; }; $wherefinal = substr($where, 0, strrpos($where, " or "));
and when echoed, $wherefinal
displays:
(species_opt.so_option_id = 4) or (species_opt.so_option_id = 12) or (species_opt.so_option_id = 17) or (species_opt.so_option_id = 20) or (species_opt.so_option_id = 21) or (species_opt.so_option_id = 32)
$v
value of radio button form generated via different sql statement , smarty.
first thing first, need construct placeholder part
$placeholder = str_repeat('?,', count($_post) - 1) . '?';
then use construct full sql query
$sql= "select species.species_id species join ( select species.species_id, count(*) mynum species_opt left join species on (species.species_id = species_opt.so_species_id) species_opt.so_option_id in ({$placeholder}) group so_species_id having mynum = 6 ) mytable on species.species_id = mytable.species_id";
after that, can bind value each placeholder
$sh = $pdo->prepare($sql); $i = 1; foreach($_post $value) { $sh->bindvalue($i, $value); $i++; } $sh->execute();
Comments
Post a Comment