⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sql.php

📁 apache windows下的一款好
💻 PHP
字号:
<?php
// $Horde: horde/lib/Log/sql.php,v 1.12 2000/08/16 20:27:34 chuck Exp $

require_once 'DB.php';

/**
 * The Log_sql class is a concrete implementation of the Log::
 * abstract class which sends messages to an SQL server.  Each entry
 * occupies a separate row in the database.
 *
 * This implementation uses PHP's PEAR database abstraction layer.
 *
 * CREATE TABLE log_table (
 *  unixtime    int NOT NULL,
 *  ident       char(16) NOT NULL,
 *  priority    int,
 *  message     varchar(200),
 *  primary key (unixtime, ident)
 * );
 *
 * @author  Jon Parise <jon@csh.rit.edu>
 * @version $Revision: 1.1 $
 * @since   Horde 1.3
 */
class Log_sql extends Log {

    // {{{ properties

    /** Array containing the dsn information. */
    var $dsn = '';

    /** Object holding the database handle. */
    var $db = '';

    /** String holding the database table to use. */
    var $table = 'log_table';

    /** Boolean indicating the current connection state. */
    var $opened = false;

    // }}}

    // {{{ constructor
    /**
     * Constructs a new sql logging object.
     *
     * @param $log_name     The target SQL table.
     * @param $ident        (optional) The identification field.
     * @param $conf         The connection configuration array.
     */
    function Log_sql($log_name, $ident = '', $conf)
    {
        $this->table = $log_name;
        $this->ident = $ident;
        $this->dsn = $conf['dsn'];
    }
    // }}}

    // {{{ open()
    /**
     * Opens a connection to the database, if it has not already
     * been opened. This is implicitly called by log(), if necessary.
     *
     * @return              True on success, false on failure.
     */
    function open()
    {
        if (!$this->opened) {
            $this->db = &DB::connect($this->dsn, true);
            if (DB::isError($this->db) || DB::isWarning($this->db)) {
                return false;
            }
            $this->opened = true;
        }
        return true;
    }
    // }}}

    // {{{ close()
    /**
     * Closes the connection to the database, if it is open.
     *
     * @return              True on success, false on failure.
     */
    function close()
    {
        if ($this->opened) {
            $this->opened = false;
            return $this->db->disconnect();
        }
        return true;
    }
    // }}}

    // {{{ log()
    /**
     * Inserts $message to the currently open database.  Calls open(),
     * if necessary.  Also passes the message along to any Log_observer
     * instances that are observing this Log.
     *
     * @param $message  The textual message to be logged.
     * @param $priority (optional) The priority of the message.  Valid
     *                  values are: LOG_EMERG, LOG_ALERT, LOG_CRIT,
     *                  LOG_ERR, * LOG_WARNING, LOG_NOTICE, LOG_INFO,
     *                  and LOG_DEBUG. The default is LOG_INFO.
     */
    function log($message, $priority = LOG_INFO)
    {
        if (!$this->opened) $this->open();

        $timestamp = time();
        $q = "insert into $this->table
              values($timestamp, '$this->ident', $priority, '$message')";
        $this->db->query($q);
        $this->notifyAll(array('priority' => $priority, 'message' => $message));
    }
    // }}}
}

?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -