我使用MVC
编写登录系统脚本的tis代码这是数据库类
<?php
class Database
{
private $host;
private $user;
private $password;
private $database;
function __construct($filename)
{
if (is_file($filename))include $filename;
else throw new Exception("Error!");
$this->host =$host;
$this->user =$user;
$this->password =$password;
$this->database =$database;
$this->connect();
}
//Connect the server
private function connect()
{
$conn=mysqli_connect($this->host, $this->user, $this->password, $this->database);
//Check Connection
if(!$conn)
throw new Exception("Error: not connect to the server");
// Select the database
if(!mysqli_select_db($conn,$this->database))
throw Exception("Error!");
}
//Close dataabase connection
function close()
{
mysqli_close();
}
}
?>
当我调用此代码时
<?php
/*
//Site login Controller
try {
include './models/database.php';
$vars= 'include/vars.php';
new database($vars);
}
catch (Exception $exc)
{
echo $exc->getMessage();
}
*/
/////////////////////login code start
if ($_POST)
{
if (isset($_POST['submit']) AND $_POST('submit') == "login" ) ;
{
$data['email'] = $_POST['email'];
$data1['password'] = $_POST['password'];
try
{
include './models/login.php';
$login = new login($data, $data1);
if($login == TRUE)
{
session_start();
$_SESSION['loggedin']='12545526465';
header('Location:index.php');
}
}
catch (Eception $exc)
{
echo $exc->getMessage();
}
}
}
?>
它给了我这个
致命错误:无法声明类数据库,因为该名称已在第5行的................. \ database.php中使用
答案 0 :(得分:5)
您不能在课程中包含相同文件的几次。您必须只包含一次。所以你改为include_once
或更好的解决方案:在一些主文件中只包含一次该文件,并为所有应用程序仅创建一次db对象