From: Lo?c C. <lo...@us...> - 2001-03-29 23:01:38
|
Update of /cvsroot/phpmychat/phpMyChat-0.15/chat/lib/database In directory usw-pr-cvs1:/tmp/cvs-serv9865/chat/lib/database Added Files: pgsql.lib.php3 odbc.lib.php3 oci8.lib.php3 mysql.lib.php3 Log Message: First drafts for the 0.15 release --- NEW FILE --- <?php // // +--------------------------------------------------------------------------+ // | phpMyChat version 0.15.0 | // +--------------------------------------------------------------------------+ // | Copyright (c) 2000-2001 The phpHeaven-team | // +--------------------------------------------------------------------------+ // | This library sets an object to access a PostgreSQL database. It uses | // | default configuration if no parameters are given to the constructor. | // | | // | usage: - include this library to the top of your files | // | - create new database handlers like this: | // | $dbHandler = new DB; | // | or | // | $dbHandler = new DB($dbHost, $dbName, $dbUser, $dbPass); | // +--------------------------------------------------------------------------+ // | From the phpMyChat project: | // | http://www.phpheaven.net/projects/phpMyChat/ | // | | // | Authors: the phpHeaven-team <php...@ya...> | // +--------------------------------------------------------------------------+ // // $Id: pgsql.lib.php3,v 1.1 2001/03/29 22:52:17 loic1 Exp $ // // PostgreSQL database handler for PHP3 and PHP4. // if (!defined('_LIB_PMCDB_LOADED')) { define('_LIB_PMCDB_LOADED', true); /** * CONFIGURATION: Whether to display error messages or not * * @const _LIB_PGSQL_SHOW_ERROR_MESSAGES */ define('_LIB_PMCDB_PGSQL_SHOW_ERROR_MESSAGES', true); /** * This library sets an object to access a PostgeSQL database. It uses * default configuration if no parameters are given to the constructor. * * @access public * @version 0.3.0 * @package phpMyChat * @author the phpHeaven-team <php...@eg...> * @author Martin Dvorak <je...@pe...> * @copyright the phpHeaven-team */ class pmcDB { /** * Hostname of the PostgreSQL server * * @var string $dbHost */ var $dbHost = C_DB_HOST; /** * Logical database name on that server * * @var string $dbName */ var $dbName = C_DB_NAME; /** * Database authorized user * * @var string $dbUser */ var $dbUser = C_DB_USER; /** * User's password * * @var string $dbPass */ var $dbPass = C_DB_PASS; /** * Last result of pg_Connect() * * @var string $linkId */ var $linkId = 0; /** * Last result of pg_exec() * * @var string $queryId */ var $queryId = 0; /** * Last record fetched * * @var array $record */ var $record = array(); /** * Current row number * * @var integer $currentRow */ var $currentRow; /** * Last error number (not supported) * * @var integer $errorNumber */ var $errorNumber = 0; /** * Last error message * * @var string $errorMessage */ var $errorMessage = ''; /** * Last error location * * @var string $errorLocation */ var $errorLocation = ''; /** * pmcDB constructor * * @param string the hostname of the PostgreSQL server * @param string the logical database name on that server * @param string the database authorized user * @param string the user's password * * @access public */ function pmcDB($dbHost = '', $dbName = '', $dbUser = '', $dbPass = '') { if (!empty($dbHost)) $this->dbHost = $dbHost; if (!empty($dbName)) $this->dbName = $dbName; if (!empty($dbUser)) $this->dbUser = $dbUser; if (!empty($dbPass)) $this->dbPass = $dbPass; } // end of the 'pmcDB()' method /** * Handle and display error messages * * @param string the error localtion * * @access private */ function updateError($location) { // $this->errorNumber = pg_errno(); // not supported $this->errorMessage = pg_errormessage(); $this->errorLocation = $location; if ($this->errorMessage && _LIB_PMCDB_PGSQL_SHOW_ERROR_MESSAGES) { echo('<br /><b>PostgreSQL error:</b> ' . $this->errorMessage . "\n"); echo('<br /><b>With:</b> ' . $this->errorLocation . "\n"); echo('<br /><b>At:</b> ' . basename($GLOBALS['PHP_SELF']) . "\n"); die('<br /><br />Session halted.' . "\n"); die('<br />Session halted.' . "\n"); flush(); } } // end of the 'updateError()' method /** * Do connect to the database * * @return boolean whether connection succeed or not * * @access private */ function connect() { if ($this->linkId == 0) { $options = 'dbname=' . $this->dbName; if ($this->dbHost) { $tmp = split(':', $this->dbHost); if ($tmp[0]) { $options .= ' host=' . $tmp[0]; if ($tmp[1]) $options .= ' port=' . $tmp[1]; } } if ($this->dbUser) $options .= ' user=' . $this->dbUser; if ($this->dbPass) $options .= ' password=' . $this->dbPass; $this->linkId = @pg_Connect($options); if (!$this->linkId) { $this->updateError('pmcDB::connect() - pg_Connect'); return false; } } return true; } // end of the 'connect()' method /** * Do run a database query * * @param string the query to run * * @return integer the query ID (0 if the query failed) * * @access public */ function query($queryString) { $this->connect(); $this->queryId = @pg_exec($this->linkId, $queryString); $this->currentRow = 0; if (!$this->queryId) { $this->updateError('pmcDB::query(' . $queryString . ') - pg_exec'); return 0; } return $this->queryId; } // end of the 'query()' method /** * Get the next record * * @return mixed the next record in an array if it exists, 'false' * else * * @access public * * @see pmcDB::query() */ function nextRecord() { $this->record = @pg_fetch_array($this->queryId, $this->currentRow); if (!$this->record || !is_array($this->record)) { $this->updateError('pmcDB::nextRecord() - pg_fetch_array'); @pg_freeresult($this->queryId); $this->queryId = 0; return false; } $this->currentRow++; return $this->record; } // end of the 'nextRecord()' method /** * Get the number of rows returned by the last select query * * @return integer the number of matching records * * @access public */ function numRows() { return ($this->queryId) ? @pg_numrows($this->queryId) : 0; } // end of the 'numRows()' method /** * Get the number of affected rows returned by the last update or * delete query * * @return integer the number of affected records * * @access public */ function affectedRows() { return ($this->queryId) ? @pg_cmdtuples($this->queryId) : 0; } // end of the 'affectedRows()' method /** * Optimize a database table * * @return boolean whether something has been done or not * * @access public */ function optimize($tableName) { $this->connect(); if ($this->linkId) { $optimized = $this->query('VACUUM ' . $tableName); } else { $optimized = 0; } return ($optimized > 0); } // end of the 'optimize()' method /** * Clean the result of a query from the memory * * @return boolean whether something has been done or not * * @access public */ function cleanResults() { if ($this->queryId) { @pg_FreeResult($this->queryId); return true; } return false; } // end of the 'cleanResults()' method /** * Close the link to the database * * @return boolean whether something has been done or not * * @access public */ function close() { if ($this->linkId) { @pg_close($this->linkId); return true; } return false; } // end of the 'close()' method } // end of the 'pmcDB' class } ?> --- NEW FILE --- <?php // // +--------------------------------------------------------------------------+ // | phpMyChat version 0.15.0 | // +--------------------------------------------------------------------------+ // | Copyright (c) 2000-2001 The phpHeaven-team | // +--------------------------------------------------------------------------+ // | This library sets an object to access an ODBC database. It uses default | // | configuration if no parameters are given to the constructor. | // | | // | usage: - include this library to the top of your files | // | - create new database handlers like this: | // | $dbHandler = new pmcDB; | // | or | // | $dbHandler = new pmcDB($dbHost, $dbName, $dbUser, $dbPass); | // +--------------------------------------------------------------------------+ // | From the phpMyChat project: | // | http://www.phpheaven.net/projects/phpMyChat/ | // | | // | Authors: J. "Guli" Mikulas <gu...@em...> | // | the phpHeaven-team <php...@ya...> | // +--------------------------------------------------------------------------+ // // $Id: odbc.lib.php3,v 1.1 2001/03/29 22:52:17 loic1 Exp $ // // ODBC database handler for PHP3 and PHP4. // if (!defined('_LIB_PMCDB_LOADED')) { define('_LIB_PMCDB_LOADED', true); /** * CONFIGURATION: Whether to display error messages or not * * @const _LIB_PMCDB_ODBC_SHOW_ERROR_MESSAGES */ define('_LIB_PMCDB_ODBC_SHOW_ERROR_MESSAGES', true); /** * This library sets an object to access an ODBC database. It uses default * configuration if no parameters are given to the constructor. * * @access public * @version 0.3.0 * @package phpMyChat * @author the phpHeaven-team <php...@eg...> * @author J. "Guli" Mikulas <gu...@em...> * @copyright the phpHeaven-team */ class pmcDB { /** * Hostname of the ODBC server * * @var string $dbHost */ var $dbHost = C_DB_HOST; /** * Logical database name on that server * * @var string $dbName */ var $dbName = C_DB_NAME; /** * Database authorized user * * @var string $dbUser */ var $dbUser = C_DB_USER; /** * User's password * * @var string $dbPass */ var $dbPass = C_DB_PASS; /** * Type of cursor to be used for this connection * * @var integer $useOdbcCursor */ var $useOdbcCursor = 0; /** * Whether to automatically free results or not (1/0) * * @var integer $autoFree */ var $autoFree = 1; /** * Last result of odbc_connect() * * @var string $linkId */ var $linkId = 0; /** * Last result of odbc_exec() * * @var string $queryId */ var $queryId = 0; /** * Last record fetched * * @var array $record */ var $record = array(); /** * Current row number * * @var integer $currentRow */ var $currentRow; /** * Last error number * * @var integer $errorNumber */ var $errorNumber = 0; /** * Last error message * * @var string $errorMessage */ var $errorMessage = ''; /** * Last error location * * @var string $errorLocation */ var $errorLocation = ''; /** * pmcDB constructor * * @param string the hostname of the ODBC server * @param string the logical database name on that server * @param string the database authorized user * @param string the user's password * * @access public */ function pmcDB($dbHost = '', $dbName = '', $dbUser = '', $dbPass = '') { if (!empty($dbHost)) $this->dbHost = $dbHost; if (!empty($dbName)) $this->dbName = $dbName; if (!empty($dbUser)) $this->dbUser = $dbUser; if (!empty($dbPass)) $this->dbPass = $dbPass; } // end of the 'pmcDB()' method /** * Handle and display error messages * * @param string the error localtion * * @access private */ function updateError($location) { // --- Removed by loic --- // $this->errorNumber = 1; // $this->errorMessage = 'General Error (The ODBC interface cannot return detailed error messages).'; // ----------------------- $this->errorNumber = odbc_error(); $this->errorMessage = odbc_errormsg(); $this->errorLocation = $location; if ($this->errorNumber && _LIB_PMCDB_ODBC_SHOW_ERROR_MESSAGES) { echo('<br /><b>ODBC error:</b> (' . $this->errorNumber . ') ' . $this->errorMessage . "\n"); echo('<br /><b>With:</b> ' . $this->errorLocation . "\n"); echo('<br /><b>At:</b> ' . basename($GLOBALS['PHP_SELF']) . "\n"); die('<br /><br />Session halted.' . "\n"); flush(); } } // end of the 'updateError()' method /** * Do connect to the database * * @return boolean whether connection succeed or not * * @access private */ function connect() { if (0 == $this->linkId) { $this->linkId = @odbc_connect($this->dbName, $this->dbUser, $this->dbPass, $this->useOdbcCursor); if (!$this->linkId) { $this->updateError('pmcDB::connect() - odbc_connect'); return false; } } return true; } // end of the 'connect()' method /** * Do run a database query * * @param string the query to run * * @return integer the query ID (0 if the query failed) * * @access public * * @todo find a workaround for the 'LIMIT' clause */ function query($queryString) { // remove the 'LIMIT' clause from the query $pos = strpos($queryString, ' LIMIT '); if ($pos) $queryString = substr($queryString, 0, $pos); unset($pos); $this->connect(); // echo('<br />Debug: query = ' . $queryString . '<br /> . "\n"); // re...@ne... suggested that we use this instead of the // odbc_exec(). // He is on NT, connecting to a Unix MySQL server with ODBC. // -- KK // $this->queryId = odbc_prepare($this->linkId, $queryString); // $this->queryOk = odbc_execute($this->queryId); $this->queryId = @odbc_exec($this->linkId, $queryString); $this->currentRow = 0; odbc_binmode($this->queryId, 1); odbc_longreadlen($this->queryId, 4096); if (!$this->queryId) { $this->updateError('pmcDB::query(' . $queryString . ') - odbc_exec'); return 0; } return $this->queryId; } // end of the 'query()' method /** * Get the next record * * @return mixed the next record in an array if it exists, 'false' * else * * @access public * * @see pmcDB::query() */ function nextRecord() { $this->$stat = @odbc_fetch_into($this->queryId, ++$this->row, &$this->record); if (!$this->$stat) { $this->updateError('pmcDB::nextRecord() - odbc_fetch_into'); if ($this->autoFree) @odbc_free_result($this->queryId); $this->queryId = 0; return false; } else { // add to record[<key>] $count = @odbc_num_fields($this->queryId); for ($i = 1; $i <= $count; $i++) $this->record[strtolower(odbc_field_name($this->queryId, $i))] = $this->record[$i - 1]; } $this->currentRow++; return $this->record; } // end of the 'nextRecord()' method /** * Set the current row number * * @param integer the current row number * * @access public */ function seek($pos) { $this->currentRow = $pos; } // end of the 'seek()' method /** * Get informations about a table * * @param string the table name * * @return mixed false if the command failed, an array containing * metadata informations else * * @access public */ function metadata($table) { $count = 0; $id = 0; $res = array(); $this->connect(); $id = @odbc_exec($this->linkId, 'SELECT * FROM ' . $table); if (!$id) { $this->updateError('pmcDB::metadata(' . $table . ') - odbc_exec'); return false; } $count = odbc_num_fields($id); for ($i = 1; $i <= $count; $i++) { $res[$i]['table'] = $table; $name = odbc_field_name($id, $i); $res[$i]['name'] = $name; $res[$i]['type'] = odbc_field_type($id, $name); $res[$i]['len'] = 0; // can we determine the width of // this column? $res[$i]['flags'] = ''; // any optional flags to report? } odbc_free_result($id); return $res; } // end of the 'metadata()' method /** * Get the number of rows returned by the last query * * Many ODBC drivers don't support 'odbc_num_rows()' on 'SELECT' * statements. The workaround below is intended to be ugly! * * @return integer the number of matching records * * @access public */ function numRows() { if (!$this->queryId) return 0; $numRows = @odbc_num_rows($this->queryId); // Workaround starts here. if ($numRows < 0) { $i = 10; while (odbc_fetch_row($this->queryId, $i)) $i *= 10; $j = 0; while ($i != $j) { $k = $j + intval(($i - $j) / 2); if (odbc_fetch_row($this->queryId, $k)) $j = $k; else $i = $k; if (($i - $j) == 1) { if (odbc_fetch_row($this->queryId, $i)) $j = $i; else $i = $j; } } $numRows = $i; } return $numRows; } // end of the 'numRows()' method /** * Get the number of fields returned by the last select query * * @return integer the number of fields * * @access public */ function numFields() { return count($this->record) / 2; } // end of the 'numFields()' method /** * Get the value of a field for a perticular record * * @return mixed the value of the field if it exists, false else * * @access public */ function fieldValue($fieldName) { return (isset($this->record[strtolower($fieldName)])) ? $this->record[strtolower($fieldName)] : false; } // end of the 'fieldValue()' method /** * Get the number of affected rows returned by the last update or * delete statement * * @return integer the number of affected records * * @access public * * @see pmcDB::numRows() */ function affectedRows() { return numRows(); } // end of the 'affectedRows()' method /** * Optimize a database table * * @return boolean whether something has been done or not * * @access public * * @todo the whole function! */ function optimize($tableName) { $this->connect(); if ($this->linkId) { // TODO // $optimized = $this->query('OPTIMIZE TABLE ' . $tableName); $optimized = 0; } else { $optimized = 0; } return ($optimized > 0); } // end of the 'optimize()' method /** * Clean the result of a query from the memory * * @return boolean whether something has been done or not * * @access public */ function cleanResults() { if ($this->queryId) { @odbc_freeresult($this->queryId); return true; } return false; } // end of the 'cleanResults()' method /** * Close the link to the database * * @return boolean whether something has been done or not * * @access public */ function close() { if ($this->linkId) { @odbc_close($this->linkId); return true; } return false; } // end of the 'close()' method } // end of the 'pmcDB' class } ?> --- NEW FILE --- <?php // // +--------------------------------------------------------------------------+ // | phpMyChat version 0.15.0 | // +--------------------------------------------------------------------------+ // | Copyright (c) 2000-2001 The phpHeaven-team | // +--------------------------------------------------------------------------+ // | This library sets an object to access an OCI8 database. It uses default | // | configuration if no parameters are given to the constructor. | // | | // | usage: - include this library to the top of your files | // | - create new database handlers like this: | // | $dbHandler = new pmcDB; | // | or | // | $dbHandler = new pmcDB($dbHost, $dbName, $dbUser, $dbPass); | // +--------------------------------------------------------------------------+ // | From the phpMyChat project: | // | http://www.phpheaven.net/projects/phpMyChat/ | // | | // | Authors: Guillaume Boddaert <gbo...@e-...> | // | Catherine Lam <cl...@e-...> | // | the phpHeaven-team <php...@ya...> | // +--------------------------------------------------------------------------+ // // $Id: oci8.lib.php3,v 1.1 2001/03/29 22:52:17 loic1 Exp $ // // OCI8 database handler for PHP3 and PHP4. // if (!defined('_LIB_PMCDB_LOADED')) { define('_LIB_PMCDB_LOADED', true); /** * CONFIGURATION: Whether to display error messages or not * * @const _LIB_PMCDB_OCI8_SHOW_ERROR_MESSAGES */ define('_LIB_PMCDB_OCI8_SHOW_ERROR_MESSAGES', true); /** * This library sets an object to access a OCI8 database. It uses default * configuration if no parameters are given to the constructor. * * @access public * @version 0.1.0 * @package phpMyChat * @author the phpHeaven-team <php...@eg...> * @author Guillaume Boddaert <gbo...@e-...> * @author Catherine Lam <cl...@e-...> * @copyright the phpHeaven-team */ class pmcDB { /** * Hostname of the OCI8 server * * @var string $dbHost */ var $dbHost = C_DB_HOST; /** * Logical database name on that server * * @var string $dbName */ var $dbName = C_DB_NAME; /** * Database authorized user * * @var string $dbUser */ var $dbUser = C_DB_USER; /** * User's password * * @var string $dbPass */ var $dbPass = C_DB_PASS; /** * Last result of ocipLogon() * * @var string $linkId */ var $linkId = 0; /** * Result of the most recent OciExecute() * * @var string $queryId */ var $queryId = 0; /** * Last record fetched * * @var array $record */ var $record = array(); /** * Current row number * * @var integer $currentRow */ var $currentRow; /** * Last error number * * @var integer $errorNumber */ var $errorNumber = 0; /** * Last error message * * @var string $errorMessage */ var $errorMessage = ''; /** * Last error location * * @var string $errorLocation */ var $errorLocation = ''; /** * pmcDB constructor * * @param string the hostname of the OCI8 server * @param string the logical database name on that server * @param string the database authorized user * @param string the user's password * * @access public */ function pmcDB($dbHost = '', $dbName = '', $dbUser = '', $dbPass = '') { if (!empty($dbHost)) $this->dbHost = $dbHost; if (!empty($dbName)) $this->dbName = $dbName; if (!empty($dbUser)) $this->dbUser = $dbUser; if (!empty($dbPass)) $this->dbPass = $dbPass; } // end of the 'pmcDB()' method /** * Handle and display error messages * * @param string the error localtion * * @access private */ function updateError($location) { $currentError = OCIError(); $this->errorNumber = $currentError['code']; $this->errorMessage = $currentError['message']; $this->errorLocation = $location; if ($this->errorNumber && _LIB_PMCDB_OCI8_SHOW_ERROR_MESSAGES) { echo('<br /><b>ORACLE error:</b> (' . $this->errorNumber . ') ' . $this->errorMessage . "\n"); echo('<br /><b>With:</b> ' . $this->errorLocation . "\n"); echo('<br /><b>At:</b> ' . basename($GLOBALS['PHP_SELF']) . "\n"); die('<br /><br />Session halted.' . "\n"); flush(); } } // end of the 'updateError()' method /** * Do connect to the database * * @return boolean whether connection succeed or not * * @access private */ function connect() { if (0 == $this->linkId) { // --- Changed from persistent to non-persistent mode by loic --- // $this->linkId = @OCIPlogon($this->dbUser, $this->dbPass, $this->dbName); $this->linkId = @OCILogon($this->dbUser, $this->dbPass, $this->dbName); // -------------------------------------------------------------- if (!$this->linkId) { $this->updateError('pmcDB::connect() - OCILogon'); return false; } } return true; } // end of the 'connect()' method /** * Do run a database query * * @param string the query to run * * @return integer the query ID (0 if the query failed) * * @access public */ function query($queryString) { $this->connect(); $this->queryId = @OCIParse($this->linkId, $queryString); $this->currentRow = 0; if (!$this->queryId) { $this->updateError('pmcDB::query(' . $queryString . ') - OCIParse'); return 0; } $this->queryId = @OCIExecute($this->queryId); if (!$this->queryId) { $this->updateError('pmcDB::query(' . $queryString . ') - OCIExecute'); return 0; } return $this->queryId; } // end of the 'query()' method /** * Get the next record * * @return mixed the next record in an array if it exists, 'false' * else * * @access public * * @see pmcDB::query() */ function nextRecord() { $isResult = @OCIFetchInto($this->queryId, $this->Record, OCI_NUM+OCI_RETURN_NULLS); if (!$isResult) { $this->updateError('pmcDB::nextRecord() - OCIFetchInto'); @ocifreestatement($this->queryId); $this->queryId = 0; return false; } $this->currentRow++; return $this->record; } // end of the 'nextRecord()' method /** * Get the number of rows returned by the last select query * * @return integer the number of matching records * * @access public * * @todo the whole function! */ function numRows() { return ($this->queryId) ? @OCIrowcount($this->queryId) : 0; } // end of the 'numRows()' method /** * Get the number of affected rows returned by the last update or * delete query * * @return integer the number of affected records * * @access public */ function affectedRows() { return ($this->queryId) ? @OCIrowcount($this->queryId) : 0; } // end of the 'affectedRows()' method /** * Optimize a database table * * @return boolean whether something has been done or not * * @access public * * @todo the whole function! */ function optimize($tableName) { $this->connect(); if ($this->linkId) { // TODO // $optimized = $this->query('OPTIMIZE TABLE ' . $tableName); $optimized = 0; } else { $optimized = 0; } return ($optimized > 0); } // end of the 'optimize()' method /** * Clean the result of a query from the memory * * @return boolean whether something has been done or not * * @access public */ function cleanResults() { if ($this->queryId) { @ocifreestatement($this->queryId); return true; } return false; } // end of the 'cleanResults()' method /** * Close the link to the database * * @return boolean whether something has been done or not * * @access public */ function close() { if ($this->linkId) { @OCILogoff($this->linkId); return true; } return false; } // end of the 'close()' method } // end of the 'pmcDB' class } ?> --- NEW FILE --- <?php // // +--------------------------------------------------------------------------+ // | phpMyChat version 0.15.0 | // +--------------------------------------------------------------------------+ // | Copyright (c) 2000-2001 The phpHeaven-team | // +--------------------------------------------------------------------------+ // | This library sets an object to access a MySQL database. It uses default | // | configuration if no parameters are given to the constructor. | // | | // | usage: - include this library to the top of your files | // | - create new database handlers like this: | // | $dbHandler = new pmcDB; | // | or | // | $dbHandler = new pmcDB($dbHost, $dbName, $dbUser, $dbPass); | // +--------------------------------------------------------------------------+ // | From the phpMyChat project: | // | http://www.phpheaven.net/projects/phpMyChat/ | // | | // | Authors: the phpHeaven-team <php...@ya...> | // +--------------------------------------------------------------------------+ // // $Id: mysql.lib.php3,v 1.1 2001/03/29 22:52:17 loic1 Exp $ // // MySQL database handler for PHP3 and PHP4. // if (!defined('_LIB_PMCDB_LOADED')) { define('_LIB_PMCDB_LOADED', true); /** * CONFIGURATION: Whether to display error messages or not * * @const _LIB_PMCDB_MYSQL_SHOW_ERROR_MESSAGES */ define('_LIB_PMCDB_MYSQL_SHOW_ERROR_MESSAGES', true); /** * This library sets an object to access a MySQL database. It uses default * configuration if no parameters are given to the constructor. * * @access public * @version 0.3.0 * @package phpMyChat * @author the phpHeaven-team <php...@eg...> * @copyright the phpHeaven-team */ class pmcDB { /** * Hostname of the MySQL server * * @var string $dbHost */ var $dbHost = C_DB_HOST; /** * Logical database name on that server * * @var string $dbName */ var $dbName = C_DB_NAME; /** * Database authorized user * * @var string $dbUser */ var $dbUser = C_DB_USER; /** * User's password * * @var string $dbPass */ var $dbPass = C_DB_PASS; /** * Last result of mysql_connect() * * @var string $linkId */ var $linkId = 0; /** * Last result of mysql_query() * * @var string $queryId */ var $queryId = 0; /** * Last record fetched * * @var array $record */ var $record = array(); /** * Current row number * * @var integer $currentRow */ var $currentRow; /** * Last error number * * @var integer $errorNumber */ var $errorNumber = 0; /** * Last error message * * @var string $errorMessage */ var $errorMessage = ''; /** * Last error location * * @var string $errorLocation */ var $errorLocation = ''; /** * pmcDB constructor * * @param string the hostname of the MySQL server * @param string the logical database name on that server * @param string the database authorized user * @param string the user's password * * @access public */ function pmcDB($dbHost = '', $dbName = '', $dbUser = '', $dbPass = '') { if (!empty($dbHost)) $this->dbHost = $dbHost; if (!empty($dbName)) $this->dbName = $dbName; if (!empty($dbUser)) $this->dbUser = $dbUser; if (!empty($dbPass)) $this->dbPass = $dbPass; } // end of the 'pmcDB()' method /** * Handle and display error messages * * @param string the error localtion * * @access private */ function updateError($location) { $this->errorNumber = mysql_errno(); $this->errorMessage = mysql_error(); $this->errorLocation = $location; if ($this->errorNumber && _LIB_PMCDB_MYSQL_SHOW_ERROR_MESSAGES) { echo('<br /><b>MySQL error:</b> (' . $this->errorNumber . ') ' . $this->errorMessage . "\n"); echo('<br /><b>With:</b> ' . $this->errorLocation . "\n"); echo('<br /><b>At:</b> ' . basename($GLOBALS['PHP_SELF']) . "\n"); die('<br /><br />Session halted.' . "\n"); flush(); } } // end of the 'updateError()' method /** * Do connect to the database * * @return boolean whether connection succeed or not * * @access private */ function connect() { if ($this->linkId == 0) { $this->linkId = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPass); if (!$this->linkId) { $this->updateError('pmcDB::connect() - mysql_connect'); return false; } $selectResult = @mysql_select_db($this->dbName, $this->linkId); if (!$selectResult) { $this->updateError('pmcDB::connect() - mysql_select_db'); return false; } } return true; } // end of the 'connect()' method /** * Do run a database query * * @param string the query to run * * @return integer the query ID (0 if the query failed) * * @access public */ function query($queryString) { $this->connect(); $this->queryId = @mysql_query($queryString, $this->linkId); $this->currentRow = 0; if (!$this->queryId) { $this->updateError('pmcDB::query(' . $queryString . ') - mysql_query'); return 0; } return $this->queryId; } // end of the 'query()' method /** * Get the next record * * @return mixed the next record in an array if it exists, 'false' * else * * @access public * * @see pmcDB::query() */ function nextRecord() { $this->record = @mysql_fetch_array($this->queryId); if (!$this->record || !is_array($this->record)) { $this->updateError('pmcDB::nextRecord() - mysql_fetch_array'); @mysql_free_result($this->queryId); $this->queryId = 0; return false; } $this->currentRow++; return $this->record; } // end of the 'nextRecord()' method /** * Get the number of rows returned by the last select query * * @return integer the number of matching records * * @access public */ function numRows() { return ($this->queryId) ? @mysql_num_rows($this->queryId) : 0; } // end of the 'numRows()' method /** * Get the number of affected rows returned by the last update or * delete query * * @return integer the number of affected records * * @access public */ function affectedRows() { return ($this->linkId) ? @mysql_affected_rows($this->linkId) : 0; } // end of the 'affectedRows()' method /** * Optimize a database table * * @return boolean whether something has been done or not * * @access public */ function optimize($tableName) { $this->connect(); if ($this->linkId) { $optimized = $this->query('OPTIMIZE TABLE ' . $tableName); } else { $optimized = 0; } return ($optimized > 0); } // end of the 'optimize()' method /** * Clean the result of a query from the memory * * @return boolean whether something has been done or not * * @access public */ function cleanResults() { if ($this->queryId) { @mysql_freeresult($this->queryId); return true; } return false; } // end of the 'cleanResults()' method /** * Close the link to the database * * @return boolean whether something has been done or not * * @access public */ function close() { if ($this->linkId) { @mysql_close($this->linkId); return true; } return false; } // end of the 'close()' method } // end of the 'pmcDB' class } ?> |