Source for file mcpear_class.php
Documentation is available at mcpear_class.php
* Simple PEAR DB wrapper class.
* This file contains the {@link mcpear} class. It is based on the standard
* PHP PEAR DB library. For more information, see {@link http://pear.php.net}
* This file is part of HRDIODB.
* HRDIODB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* HRDIODB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with HRDIODB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* A PEAR DB based class for interacting with a SQL database.
* Generic PEAR DB function wrappers used for various DB operations. PEAR is
* a very handy and easy to use library, but having some wrappers makes using
* it even easier and faster. This also allows for central error checking and
* handy utility functions. For more information about PEAR, please see
* {@link http://pear.php.net}.
* Establishes database connection.
* @param string $dsn Data Source Name
function mcpear ($dsn = FALSE) {
// Set default fetch mode to associative
$this->db->setFetchMode(DB_FETCHMODE_ASSOC);
* Initializes a PEAR database connection.
* Textbook PEAR database connection function.
* @return mixed PEAR DB Database connection
return error("Your DB class constructor must set \$this->dsn");
/* Include the PEAR DB library. It should be available with every PHP
installation, unless it was specifically excluded. On package-based
Linux distributions, check to see if the PHP PEAR package is installed.
For more information, see http://pear.php.net */
// Initialize the PEAR connection
$db = DB::connect($this->dsn);
// Check to make sure we didn't encounter an error
* General database query method.
* Textbook PEAR database query. We use getuserinfo() for error
* @param string $sql SQL query
* @return mixed Query result resource or FALSE on error
$q = & $this->db->query($sql);
* Retrieves a row of data.
* Returns a row of data for the given SQL query.
* @param string $sql SQL query
* @return mixed Row of data or FALSE on error
$row = & $this->db->getRow($sql);
* Retrieves a list of data.
* Returns a list of data for the given SQL query.
* @param string $sql SQL query
* @return mixed List of results or FALSE on error
$results = & $this->db->getAll($sql);
return $this->verify($results);
* Retrieves a column of data.
* Retrieves a column of data composed of the first row of the results for
* @param string $sql SQL query
* @return mixed Column of results or FALSE on error
$results = & $this->db->getCol($sql);
return $this->verify($results);
* Retrieves a single value.
* Retrieves the value of the first result for the given SQL query.
* @param string $sql SQL query
* @return mixed Value of the first result or FALSE on error
$results = & $this->db->getOne($sql);
return $this->verify($results);
* Retrieves an array of field names.
* Retrieves an array of field names in a given table.
* @param string $table SQL query
* @return mixed An array of field names for a given table or FALSE on error
$results = $this->db->tableInfo($table);
$tableinfo = $this->verify($results);
foreach($tableinfo as $t){
$field_names[] = $t['name'];
* Returns table row count.
* Returns the total number of rows in a given table.
* @param string $table Table name
* @return mixed Number of rows in the table or FALSE on error
$total = & $this->db->getOne($sql);
* Returns table contents.
* Returns an array of associative arrays for the given SQL query.
* @param string $table Table name
* @return mixed Associative array of results or FALSE on error
* Returns next insert id.
* Returns the next insert id for the sequence $name. Depending on the
* database you're using, native sequences may or may not exist. PEAR tries
* to handle things equally by using native sequences in databases which
* support them, or creating sequence tables in those who don't. By default,
* PEAR will add the string "_seq" to any sequence name. Keep that in mind
* when asking it for the next sequence number.
* @param string $name The name of the sequence
* @return integer Next id sequence number
$id = & $this->db->nextId($name);
* Adds a tabe row to the specific $table using the keys of the $values
* array as field names and the values as values for those fields.
* @param array $values Associative array of keys and values
* @param string $table Table name
* @return mixed Query result resource or FALSE on error
function add ($values, $table) {
// Unset any values which should be NULL
foreach($values as $key=> $value){
// Auto-execute the INSERT query
$q = $this->db->autoExecute($table, $values, DB_AUTOQUERY_INSERT);
return error("Add expects an array of data");
* Updates table rows for the specific $table using the keys of the $values
* array as field names and the values as values for those fields.
* @param array $values Associative array of keys and values
* @param string $table Table name
* @param string $where_key A key into the $values array used for the WHERE
* @return mixed Query result resource or FALSE on error
function update ($values, $table, $where_key) {
// Create the WHERE clause
$update_key = "$where_key = '{$values[$where_key]}'";
// Set Null any values that say NULL
foreach($values as $key=> $value){
// Auto-execute the UPDATE query
$q = $this->db->autoExecute($table, $values, DB_AUTOQUERY_UPDATE, $update_key);
* Deletes table rows from a specific $table using the value of the $values
* array's $where_key key as field name and value as value for the WHERE
* @param array $values Associative array of keys and values
* @param string $table Table name
* @param string $where_key A key into the $values array used for the WHERE
* @return mixed Query result resource or FALSE on error
function delete ($values, $table, $where_key) {
$sql = "DELETE FROM $table
WHERE $where_key = '{$values[$where_key]}'";
* Returns the result or prints an error.
* This function is meant to be used in the 'return' statement of all mcpear
* functions. It checks to see if there is a valid result or a DB error. If
* the result is valid, it is returned. If there is a DB error, it is printed
* @param mixed $result DB result
* @return mixed Query result or FALSE on error
if (DB::isError($result)){
return error($result->getMessage());
|