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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -