note 95131 deleted from book.pdo by danbrown

From: Date: Tue, 15 Dec 2009 03:29:06 +0000
Subject: note 95131 deleted from book.pdo by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163983@lists.php.net to get a copy of this message
Note Submitter: Raymond ---- There are times when you want to quickly retrieve data from a table. Here's a class that I've created to do just that: class RaxanPDO extends PDO { /*** * Retrieve records from a database table * @returns Array or false on error */ public function table($name,$filterClause = null, $filterValues = null) { // get field names $fields = '*'; if (($p = strpos($name,' '))!==false) { $fields = substr($name,$p); $name = substr($name,0,$p); } $sql = 'select '.$fields.' from '.$name; if ($filterClause==null) $ds = $this->query($sql); else { $sql.= ' where '.$filterClause; $ds = $this->prepare($sql); if ($filterValues!==null && !is_array($filterValues)) $filterValues = array_slice(func_get_args(),2); // use arguments as input $ds->execute($filterValues); } if ($ds===false) return false; else return $ds->fetchAll(PDO::FETCH_ASSOC); } } Usage: // retrieve all records from employees table $rows = $db->table('employees'); To learn more about the RaxanPDO class and other useful APIs visit: http://raxanpdi.com/docs/table-of-contents.html

« previous php.notes (#163983) next »