First, I assume that you are using PDO.
Try to execute
class App_Model_TableName { public $status; public $zip; // public $other_column; } class YourClass { protected function getAdapter() { // Do adapter stuffs } public function query($query, array $param) { // When Using PDO always use prepare and execute when you pass in a variable // This will help prevent SQL injection $stmt = $this->getAdapter()->prepare($query); return $query->execute($param); } /** * @return App_Model_TableName[] */ public function getRowsByZipCode($zip) { // SQL to get all the rows with the given zip code // This way will help prevent SQL injection $query = "SELECT * FROM table_name WHERE table_name.status = 1 AND table_name.zip = :zip"; $qData = array(':zip' => $zip); $results = $this->query($query, $qData); return $results->fetchAll(PDO::FETCH_CLASS, 'App_Model_TableName'); } }
Calling YourClass::getRowsByZipCode() will then return you an array of App_Model_TableName objects. Then you can access them, for example:
$data = $instance_of_yourclass->getRowsByZipCode(12345); foreach ($data as $row) { echo $row->zip; echo $row->do_stuff(); }
All these amazing features that I found on:
Disclaimer: This code has not been tested :(
Be healthy, but stay warm.
source share