note 95131 added to book.pdo
| From: | Raymond at osu1 dot php dot net | Date: | Tue, 15 Dec 2009 01:40:02 +0000 |
| Subject: | note 95131 added to book.pdo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163973@lists.php.net to get a copy of this message | ||
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
----
Server IP: 190.15.66.25
Probable Submitter: 72.27.145.33
----
Manual Page -- http://www.php.net/manual/en/book.pdo.php
Edit -- https://master.php.net/note/edit/95131
Del: integrated -- https://master.php.net/note/delete/95131/integrated
Del: useless -- https://master.php.net/note/delete/95131/useless
Del: bad code -- https://master.php.net/note/delete/95131/bad+code
Del: spam -- https://master.php.net/note/delete/95131/spam
Del: non-english -- https://master.php.net/note/delete/95131/non-english
Del: in docs -- https://master.php.net/note/delete/95131/in+docs
Del: other reasons-- https://master.php.net/note/delete/95131
Reject -- https://master.php.net/note/reject/95131
Search -- https://master.php.net/manage/user-notes.php