Home › My Color Database Access Object Class
My Color Database Access Object Class
class dbMy_color { const VOTE_SQL= "select name, count(id) as num_votes from {my_color} group by name order by num_votes desc"; function __construct() { } public function add($myColor) { $fields = array('name' => trim($myColor->name)); $dbResult= db_insert('my_color')->fields($fields)->execute(); // Note! $dbResult contains the serial field (autogen) int value created by the // database engine return $dbResult; } public function get($id = NULL) { $dbResult = db_query('select name from {my_color} where id = :id', array(':id'=>$id))->fetchObject(); if ($dbResult == NULL) { throw new Exception('No value found'); } else { $name = $dbResult->name; return $name; } } public function getCountByColor() { $stmnt = db_query(dbMy_color::VOTE_SQL); if ($stmnt == null) throw new Exception('Unexpected Database Exception Occurred'); $rows= array(); while($vote = $stmnt->fetch(PDO::FETCH_OBJ)) { $col = COLOR_SPAN_BEGIN.$vote->name.COLOR_SPAN_END.$vote->name; $rows[] = array($col, $vote->num_votes); } if (count($rows) == 0) throw new Exception('No Records Found'); return $rows; } }



