我已经看到了提出的问题,但答案对我来说不是很清楚。 我的代码是。
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();
?>
连接.php
<?php
class Database {
public function __construct() {
$host = 'localhost';
$user = 'root';
$pass = '';
$name = 'meeboo3';
$this->mysqli = new mysqli($host, $user, $pass, $name);
}
}
?>
Database 类没有被调用两次?那么怎么会出现错误呢?谁能在评论中解释一下原因。
最佳答案
您可以在这样做之前测试一下它是否已经声明:
if (!isset($database) && !is_a($database, 'Database')){
$database = new Database();
}
或者
如果您在 connect.php
中声明它,您可以:
include_once 'connect.php';
而不是
include 'connect.php';
关于php - fatal error : Cannot redeclare class Database. 如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25633390/