故障使用PHP
问题描述:
我有一个3文件连接到MySQL数据库:故障使用PHP
class.database.php
config.php
test.php
在class.database.php使用我:
<?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);
}
}
?>
在config.php我使用:
<?php
include_once 'class.database.php';
$config['hostname'] = 'localhost';
$config['dbuser'] = 'root';
$config['dbpass'] = '';
$config['dbname'] = 'test';
$db = new Database($config);
?>
而且test.php的我使用:
<?php
include 'config.php';
include 'class.database.php';
时在wampserver运行test.php的是主机警报错误是Fatal error: Cannot redeclare class Database in class.database.php on line 2
如何解决呢?
答
您是include
文件两次,这会产生此错误。总是使用include_once
,而不是include
。
你是怎么想出名字“3档案”的? – 2012-03-02 03:34:18
@l__:谢谢 – 2012-03-02 03:36:20