php - How make a Dynamic bindValue()? -
okay have function called sendquery sends query. know how bindparams, can't think of way make work bind values inside execute.
this code:
public function sendquery($query, array $value, $do_fetch) { $query_process = $this->db->prepare($query); if(!$query_process->execute($binds)) { throw new excpetion ("an error has occured!"); } $this->insert = $this->db->lastinsertid(); $this->row_count = $query_process->rowcount(); if($fetch == true) { return $query_process->fetchall(); } }
as see, executes $binds,
works (where user = ?), want send queries this: (where user = :user) instead of ' ? ', , multiple of them.
how do so?
you have same.
rid of useless code , use consistent variable naming
public function sendquery($query, array $binds, $do_fetch) { $stm = $this->db->prepare($query); $stm->execute($binds); $this->insert = $this->db->lastinsertid(); $this->row_count = $stm->rowcount(); if($do_fetch) { return $stm->fetchall(); } } $sql = "select * t c1=:name , c2=:age"; $param = array ("name" => $name,"age" => $age); $data = $db->sendquery($sql, $data, 1);
however, instead of single function create set of them:
- query() run non-select queries
- getone() preforms select , returns scalar value
- getrow() returns row
- getall returns rows
it extremely handy
Comments
Post a Comment