All pastes #22688 Raw Edit

Skudd

public php v1 · immutable
#22688 ·published 2005-09-12 01:47 UTC
rendered paste body
<?php    /**     * A MySQL interface class     *     * This class is to be used to interface with a MySQL database     * server. It depends on the $config['db'] global configuration     * array.     *     * @package DB_Class     * @version 3.0     * @author Tim Garrison <tim@skudd.com>     * @copyright Copyright &copy; 2005 - Tim Garrison     */        class db_class {        /**         * Constructor         */        function __construct($default_query = NULL) {            if($default_query != NULL) $this->Query($default_query);        }                /**         * Configuration Processor         * @param array $conf Alternate configuration         */        function Config($conf = NULL) {            if($conf == NULL) {                global $config;                $conf = $config['db'];            }                        if(is_array($conf)) {                $this->db_host = $conf['host'];                $this->db_user = $conf['user'];                $this->db_pass = $conf['pass'];                $this->db_name = $conf['name'];            } else {                $this->Log('Error: Configuration not valid', true);            }                    }                /**         * Connection Processor         */        function Connect() {            global $__dbconn;            if($__dbconn == false) {                $this->Config();                $this->db = mysql_connect($this->db_host, $this->db_user, $this->db_pass)                    or $this->Log('Error: Failed connecting to MySQL server. Server said: '.mysql_error(), true);                mysql_select_db($this->db_name, $this->db)                    or $this->Log('Error: Failed selecting database. Server said: '.mysql_error(), true);            }        }        /**         * Query Processor         * @param string $query SQL Query         */        function Query($query) {            $this->Connect();            $this->result = mysql_query($query, $this->db)                or $this->Log('Error: Query failed. Server said: '.mysql_error($this->db), false);            if(is_resource($this->result)) {                $this->num_rows = mysql_num_rows($this->result);            } else {                $this->num_rows = 0;            }        }                /**         * Multi-Row Result Processor         * @return array         */        function Result2Array() {            if(is_resource($this->result) && $this->num_rows > 0) {                while($row = mysql_fetch_assoc($this->result)) {                    $output[] = $row;                }            } else {                $output = false;            }                        return $output;        }                /**         * Single-Row Result Processor         * @return array         */        function SingleRow() {            $rows = $this->Result2Array();            if($rows != false) $output = $rows[0];            else $output = false;                        return $output;        }                /**         * Error Logging Processor         *         * @param string $error Error message         * @param boolean $fatal Fatal error message         */        function Log($error, $fatal) {            if($fatal == true) die($error);            else echo($error."<br />\n");        }                /**#@+         * Database Configuration Variable         * @access private         * @var string         */                /**         * Host         */        private $db_host;                /**         * Username         */        private $db_user;                /**         * Password         */        private $db_pass;                /**         * Database Name         */        private $db_name;                /**#@-*/                /**         * Connection Handle         * @access private         * @var resource         */        private $db;                /**         * Query Result         * @access public         * @var resource         */        public $result;                /**         * Number of Rows Returned         * @access public         * @var integer         */        public $num_rows;        }        /**     * Connection Handle     *     * In order to minimize the number of connections made to the     * database server, directly affecting the performance, this     * variable is used to maintain a connection for the execution     * of the script. It will be used by all instances of db_class.     *     * @global resource $__dbconn     */         global $__dbconn;     $__dbconn = false;?>