如何处理数据库服务器错误?

时间:2015-03-31 11:41:47

标签: php mysql

我有项目在该项目中发生错误,它说无法连接数据库 它给出了以下错误...如何处理此错误?我不知道为什么会发生这种情况.. 我将数据库导入mysql。

A Database Error Occurred

Unable to connect to your database server using the provided settings.

我的代码在

之下
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CI_DB_driver {

    var $username;
    var $password;
    var $hostname;
    var $database;
    var $dbdriver       = 'mysql';
    var $dbprefix       = '';
    var $char_set       = 'utf8';
    var $dbcollat       = 'utf8_general_ci';
    var $autoinit       = TRUE; // Whether to automatically initialize the DB
    var $swap_pre       = '';
    var $port           = '';
    var $pconnect       = FALSE;
    var $conn_id        = FALSE;
    var $result_id      = FALSE;
    var $db_debug       = FALSE;
    var $benchmark      = 0;
    var $query_count    = 0;
    var $bind_marker    = '?';
    var $save_queries   = TRUE;
    var $queries        = array();
    var $query_times    = array();
    var $data_cache     = array();
    var $trans_enabled  = TRUE;
    var $trans_strict   = TRUE;
    var $_trans_depth   = 0;
    var $_trans_status  = TRUE; // Used with transactions to determine if a rollback should occur
    var $cache_on       = FALSE;
    var $cachedir       = '';
    var $cache_autodel  = FALSE;
    var $CACHE; // The cache class object

    // Private variables
    var $_protect_identifiers   = TRUE;
    var $_reserved_identifiers  = array('*'); // Identifiers that should NOT be escaped

    // These are use with Oracle
    var $stmt_id;
    var $curs_id;
    var $limit_used;



    /**
     * Constructor.  Accepts one parameter containing the database
     * connection settings.
     *
     * @param array
     */
    function __construct($params)
    {
        if (is_array($params))
        {
            foreach ($params as $key => $val)
            {
                $this->$key = $val;
            }
        }

        log_message('debug', 'Database Driver Class Initialized');
    }

    // --------------------------------------------------------------------

    /**
     * Initialize Database Settings
     *
     * @access  private Called by the constructor
     * @param   mixed
     * @return  void
     */
    function initialize()
    {
        // If an existing connection resource is available
        // there is no need to connect and select the database
        if (is_resource($this->conn_id) OR is_object($this->conn_id))
        {
            return TRUE;
        }

        // ----------------------------------------------------------------

        // Connect to the database and set the connection ID
        $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();

        // No connection resource?  Throw an error
        if ( ! $this->conn_id)
        {
            log_message('error', 'Unable to connect to the database');

            if ($this->db_debug)
            {
                $this->display_error('db_unable_to_connect');
            }
            return FALSE;
        }

        // ----------------------------------------------------------------

        // Select the DB... assuming a database name is specified in the config file
        if ($this->database != '')
        {
            if ( ! $this->db_select())
            {
                log_message('error', 'Unable to select database: '.$this->database);

                if ($this->db_debug)
                {
                    $this->display_error('db_unable_to_select', $this->database);
                }
                return FALSE;
            }
            else
            {
                // We've selected the DB. Now we set the character set
                if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
                {
                    return FALSE;
                }

                return TRUE;
            }
        }

        return TRUE;
    }

2 个答案:

答案 0 :(得分:1)

    var $username;
    var $password;
    var $hostname;
    var $database;

这些值为空白,为上述变量提供有效值

答案 1 :(得分:0)

试试这个,

$param = array('username' => 'root', 'password' => 'password', 'hostname' => 'localhost', 'database' => 'DBNAME');

$db = new CI_DB_driver($param);

$ param数组包含将用于连接数据库的所有必需细节。