Hello,

Iam getting an fatal error " PHP Fatal error: Cannot redeclare class MysqlConnect " It was my connection file and the website was

working from the past two years. Yesterday on of my friend said my website was down so that time i noticed this issue I got the above meesage from my error_log file below I am including my view page and connection page some body please help.

View page

<?php require_once "includes/MysqlConnect.php";
$con = MysqlConnect::getInstance();
$directormessage=$con->select("SELECT * FROM bsi_director_message");
$hotlink=$con->select("SELECT * FROM bsi_hot_links");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>website title</title>
<meta name="description" content="website description" />
<meta  name="keywords" content="keyword" />
<link rel="shortcut icon" href="images/favicon.ico">
</head>
<body>
<p><strong><?php echo $directormessage['title'];?></strong></p>
</body>

Connection Page

<?php

class MysqlConnect {

    private static $_instance = false;
    private $_con;

    public function __construct() {
        //
    }

    public function __sleep() {
        //
    }

    public function __wakeup() {
        //
    }

    public static function getInstance() {
        if (self::$_instance === false) {

            $instance = new self();
            $instance->_con = mysqli_connect('localhost', 'root', 'password')
                    or trigger_error('Could not connect to database server', E_USER_ERROR);

            mysqli_select_db($instance->_con, 'database_name')
                    or trigger_error('Could not select database', E_USER_ERROR);

            self::$_instance = $instance;
        }

        return self::$_instance;
    }

    public static function dbEscape($value) {
        return self::getInstance()->_escapeString($value);
    }

    public function select($sql, $fetchAll = false) {
//echo $sql;
        $str = mysqli_query($this->_con, $sql);

        if ($fetchAll === false) {
            $row = mysqli_fetch_assoc($str);
            return $row;
        }

        $result = array();
        while (($row = mysqli_fetch_assoc($str))) {
            $result[] = $row;
        }

        return $result;
    }

    public function update($table, $data, $where) {

        $dataUpdate = array();
        foreach ($data as $field => $value) {
            $dataUpdate[] = "`" . $field . "` = " . $this->_escapeString($value);
        }

        if (is_array($where)) {
            $where = '(' . implode(') AND (', $where) . ')';
        }

        $sql = "UPDATE `" . $table . "` SET "
                . implode(',', $dataUpdate) . " WHERE " . $where;

        $result = mysqli_query($this->_con, $sql);
        return $result;
    }

    public function delete($table, $where) {

        $dataDelete = array();

        if (is_array($where)) {
            $where = '(' . implode(') AND (', $where) . ')';
        }

        $sql = "DELETE FROM `" . $table . "` WHERE " . $where;

        $result = mysqli_query($this->_con, $sql);
        return $result;
    }

    public function insert($table, $data) {

        $formattedVals = array();
        foreach (array_values($data) as $value) {
            $formattedVals[] = $this->_escapeString($value);
        }

        $sql = "INSERT INTO `" . $table . "`"
                . " (" . '`' . implode('`,`', array_keys($data)) . '`' . ")"
                . " VALUES (" . implode(",", $formattedVals) . ")";

        $result = mysqli_query($this->_con, $sql);
        return $result;
    }

    //--------------------------------------------------------------------------
    // private methods
    //--------------------------------------------------------------------------

    private function _escapeString($value) {

        if ($value === null) {
            return 'NULL';
        }

        if (is_int($value)) {
            return $value;
        }

        return "'" . mysqli_real_escape_string($this->_con, $value) . "'";
    }

}

///************Fetch Pagination*************///

$db_username = 'root';
$db_password = 'password';
$db_name = 'database_name';
$db_host = 'localhost';
$item_per_page = 8;
$item_per_pageone = 2;
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
///***********End Pagination**********///

///************ NORMAL PAGINATION CONNECTION *************///
$db_username = 'root';
$db_password = 'password';
$db_name = 'database_name';
$db_host = 'localhost';
$item_per_page = 1;
$connecDBS = mysql_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
///***********End Pagination**********///

So any one have any idea what was wrong ? as this website was working from the past two year's and now only it was showing this erro and the page was blank

The common fix or reply about this with a google search seems to be "wrap your class in code to check for the function/class."

So the entire Class MysqlConnect is wrapped like this:

if (class_exists('MysqlConnect') != true) {

    class MysqlConnect { ... }

}

`

commented: its worked +0

Thank you rproffitt , it worked as I was searching for an resolution for this issue but

the forum's and website's are saying to add required or once_required but none of them

worked for me. As it worked I added the line like below so the website started working.

Thanks..

<?php

if (class_exists('MysqlConnect') != true) { 

class MysqlConnect {
    private static $_instance = false;
    private $_con;
    public function __construct() {
        //
    }
    public function __sleep() {
        //
    }
    public function __wakeup() {
        //
    }
    public static function getInstance() {
        if (self::$_instance === false) {
            $instance = new self();
            $instance->_con = mysqli_connect('localhost', 'root', 'password')
                    or trigger_error('Could not connect to database server', E_USER_ERROR);
            mysqli_select_db($instance->_con, 'database_name')
                    or trigger_error('Could not select database', E_USER_ERROR);
            self::$_instance = $instance;
        }
        return self::$_instance;
    }
    public static function dbEscape($value) {
        return self::getInstance()->_escapeString($value);
    }
    public function select($sql, $fetchAll = false) {
//echo $sql;
        $str = mysqli_query($this->_con, $sql);
        if ($fetchAll === false) {
            $row = mysqli_fetch_assoc($str);
            return $row;
        }
        $result = array();
        while (($row = mysqli_fetch_assoc($str))) {
            $result[] = $row;
        }
        return $result;
    }
    public function update($table, $data, $where) {
        $dataUpdate = array();
        foreach ($data as $field => $value) {
            $dataUpdate[] = "`" . $field . "` = " . $this->_escapeString($value);
        }
        if (is_array($where)) {
            $where = '(' . implode(') AND (', $where) . ')';
        }
        $sql = "UPDATE `" . $table . "` SET "
                . implode(',', $dataUpdate) . " WHERE " . $where;
        $result = mysqli_query($this->_con, $sql);
        return $result;
    }
    public function delete($table, $where) {
        $dataDelete = array();
        if (is_array($where)) {
            $where = '(' . implode(') AND (', $where) . ')';
        }
        $sql = "DELETE FROM `" . $table . "` WHERE " . $where;
        $result = mysqli_query($this->_con, $sql);
        return $result;
    }
    public function insert($table, $data) {
        $formattedVals = array();
        foreach (array_values($data) as $value) {
            $formattedVals[] = $this->_escapeString($value);
        }
        $sql = "INSERT INTO `" . $table . "`"
                . " (" . '`' . implode('`,`', array_keys($data)) . '`' . ")"
                . " VALUES (" . implode(",", $formattedVals) . ")";
        $result = mysqli_query($this->_con, $sql);
        return $result;
    }
    //--------------------------------------------------------------------------
    // private methods
    //--------------------------------------------------------------------------
    private function _escapeString($value) {
        if ($value === null) {
            return 'NULL';
        }
        if (is_int($value)) {
            return $value;
        }
        return "'" . mysqli_real_escape_string($this->_con, $value) . "'";
    }
}
///************Fetch Pagination*************///
$db_username = 'root';
$db_password = 'password';
$db_name = 'database_name';
$db_host = 'localhost';
$item_per_page = 8;
$item_per_pageone = 2;
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
///***********End Pagination**********///
///************ NORMAL PAGINATION CONNECTION *************///
$db_username = 'root';
$db_password = 'password';
$db_name = 'database_name';
$db_host = 'localhost';
$item_per_page = 1;
$connecDBS = mysql_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
///***********End Pagination**********///

}

Thanks for the report back. Now there are some that will call this not a great fix but hey, sometimes you can't go over all the code and have to go with a wrap solution as I noted.

Your supplied code didn't find Waldo for me. Or rather I didn't see where you could have included this class more than one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.