2016-11-09 19 views
-2

我写那朵代码使用MVC致命错误:无法声明类数据库,因为该名称已在使用中

登录系统脚本这是类数据库

<?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行

+0

你不从错误消息中了解什么创建数据库对象一次? – 2016-11-09 10:29:27

+0

你应该改变类'Database'的名称,因为它已经开始使用你的框架! –

+0

我没有使用任何框架 – user3794383

回答

1

在.................使用\ database.php中你不能在几次与类相同的文件。您只能包含一次。所以,你include_once代替或更好的解决方案:包括这个文件只在某些主文件一劳永逸应用

+0

我不明白你这个意思,或者什么错误,我这样做,你提到要 – user3794383

+0

你包括几次相同的文件。我告诉你:不是'include'使用'include_once' – nospor

+0

你的意思是在登录代码为database.php文件? – user3794383

相关问题