PDO kullanarak fetchAll yardımcı işlevi

0 Cevap php

Ben bir işlevi var varsayalım

function fetchAll(){
  $args = func_get_args();
  $query = array_shift($args);
  $query = str_replace("%s","'%s'",$query);
  foreach ($args as $key => $val) {
    $args[$key] = mysql_real_escape_string($val);
  }
  $query = vsprintf($query, $args);
  if (!$query) return FALSE;

  $res = mysql_query($query);
  if (!$res) {
    trigger_error("db: ".mysql_error()." in ".$query);
    return FALSE;
  }
  $a = array();
  while($row = mysql_fetch_assoc($res)) $a[]=$row;
  return $a;
}

ve sonra bu gibi kullanmak

$a=$db->fetchAll("SELECT * FROM users WHERE status=%s LIMIT %d,%d",$status,$start,$num);

How can I rewrite it using PDO?
Every example I can find shows only how to bind parameters directly. Should I pass variable type as well as it's value? Or make this call always 4 lines - 3 binds and execute?

0 Cevap