PHP PDO Class Data Extraction -


hello using pdo class i've downloaded in this website , code of selecting data $results = $db->select("accounts");. , here functions in class

public function run($sql, $bind="") {     $this->sql = trim($sql);z     $this->bind = $this->cleanup($bind);     $this->error = "";      try {         $pdostmt = $this->prepare($this->sql);         if($pdostmt->execute($this->bind) !== false) {             if(preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql))                 return $pdostmt->fetchall(pdo::fetch_assoc);             elseif(preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql))                 return $pdostmt->rowcount();         }        } catch (pdoexception $e) {         $this->error = $e->getmessage();             $this->debug();         return false;     } }  public function select($table, $where="", $bind="", $fields="*") {     $sql = "select " . $fields . " " . $table;     if(!empty($where))         $sql .= " " . $where;     $sql .= ";";     return $this->run($sql, $bind); } 

my problem want echo out data in while loop i'm having problems doing it. using print_r($results) check data in database. i've tried

while($row=$results->fetch(pdo::fetch_assoc))  { echo $row['somefield']; }  

and getting error. fatal error: call member function fetch() on non-object . how can put code in while or foreach loop?

as stated in comment above, wrapped pdo class makes pdostatement::fetchall() call , returns result of db::select().

as such, need iterate on $results other array:

foreach ($results $result) {     echo $result['somefield']; } 

Comments