I have a 3 file:
class.database.php
config.php
test.php
In class.database.php i using:
<?php
class Database {
private $db_connect;
function __construct($config) {
$this->connect($config);
}
function connect($config) {
$this->db_connect = @mysql_connect($config['hostname'], $config['dbuser'], $config['dbpass']) or die("Can't connect to mysql server");
@mysql_select_db($config['dbname'], $this->db_connect) or die("Can't select database mysql server");
$this->query('set names utf8');
}
function disconnect() {
mysql_close($this->db_connect);
}
}
?>
in config.php i using:
<?php
include_once 'class.database.php';
$config['hostname'] = 'localhost';
$config['dbuser'] = 'root';
$config['dbpass'] = '';
$config['dbname'] = 'test';
$db = new Database($config);
?>
And test.php i using:
<?php
include 'config.php';
include 'class.database.php';
when to run test.php in wampserver is host alert error is Fatal error: Cannot redeclare class Database in class.database.php on line 2
How to fix it ?
You’re
includeing theclass.database.phpfile twice, which gives this error. Useinclude_oncefor it always, notinclude.