2014-09-02 88 views
0

我見過這個問題,但答案不是很清楚。 我的代碼是。致命錯誤:無法重新聲明類數據庫。怎麼修?

的index.php

<?php include 'header.php'; ?> 
    <?php 
     include "class.users.php"; 
     if(isset($_POST['login'])) { 
     $username = $_POST['username1']; 
     $password = $_POST['password1']; 
     $users->login($username, $password); 
     } 
    ?> 

class.users.php

<?php 
    include "connect.php"; 
    class Users extends Database { 
    public function login($username, $password) { 
     $stmt = $this->mysqli->prepare("SELECT username, password FROM users WHERE username = ? AND password = ? LIMIT 1"); 
     $stmt->bind_param('ss', $username, $password); 
     $stmt->execute(); 
     $stmt->bind_result($username, $password); 
     $stmt->store_result(); 
     if($stmt->num_rows == 1) { 
     while($stmt->fetch()) { 
      $_SESSION['username'] == $username; 
      header("Location: dashboard.php"); 
     } 
     } else { 
      return false; 
     } 
     $stmt->close(); 
     $stmt->free_result(); 
    } 
    } 
    $users = new users(); 
?> 

connect.php

<?php 

    class Database { 
     public function __construct() { 
     $host = 'localhost'; 
     $user = 'root'; 
     $pass = ''; 
     $name = 'meeboo3'; 
     $this->mysqli = new mysqli($host, $user, $pass, $name); 
     } 
    } 
    ?> 

類數據庫不叫兩次嗎?那麼它是一個錯誤呢?任何人都可以解釋爲什麼在評論中。

+0

'connect.php'中有什麼? – 2014-09-02 22:32:22

+0

我不知道類數據庫是否已經聲明或包含在header.php或connect.php? – 2014-09-02 22:32:32

+0

對不起,我忘了加入connect.php – logchipperman 2014-09-02 22:33:16

回答

1

,你可以測試一下,看看如果這樣做之前就已經宣佈:如果你宣稱這裏面connect.php

if (!isset($database) && !is_a($database, 'Database')){ 
    $database = new Database(); 
} 

或者

你可以:

include_once 'connect.php'; 

,而不是

include 'connect.php'; 
相關問題