未找到包含文件中的Php类

时间:2017-01-04 14:40:32

标签: php android mysql

我试图按照这个tutorial使用MySql对Android应用程序进行简单的登录和注册。 Android应用运行正常,直到访问数据库(帐户注册表)时出现错误。

当我尝试访问php应用程序以确保错误在Android应用程序中时,我收到此错误:

Fatal error: Class 'DbConnect' not found in C:\xampp\htdocs\AndroidLogin\include\user.php on line 12

我确定db.php中已包含user.php。这些是我在教程中使用的代码:第一个是index.php

//index.php
<?php

require_once 'include/user.php';

$username = "";
$password = "";
$email = "";

if(isset($_POST['username'])){
    $username = $_POST['username'];
}

if(isset($_POST['password'])){
    $password = $_POST['password'];
}

if(isset($_POST['email'])){
    $email = $_POST['email'];
}

// Instance of a User class
$userObject = new User();

// Registration of new user
if(!empty($username) && !empty($password) && !empty($email)){
    $hashed_password = md5($password);
    $json_registration = $userObject->createNewRegisterUser($username, $hashed_password, $email);
    echo json_encode($json_registration);
}

// User Login
if(!empty($username) && !empty($password) && empty($email)){
    $hashed_password = md5($password);
    $json_array = $userObject->loginUsers($username, $hashed_password);
    echo json_encode($json_array);
}
?>

接下来,config.php

//config.php
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "androidlogin");
?>

这是db.php

// db.php
<?php

include_once 'config.php';

class DbConnect{
    private $connect;

    public function __construct(){
        $this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        if (mysqli_connect_errno($this->connect)){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    }

    public function getDb(){
        return $this->connect;
    }
}
?>

最后一个是user.php

// user.php
<?php

include_once 'db.php';

class User{
    private $db;
    private $db_table = "users";

    public function __construct(){
        $this->db = new DbConnect();
    }

    public function isLoginExist($username, $password){
        $query = "select * from " . $this->db_table . " where username = '$username' AND password = '$password' Limit 1";
        $result = mysqli_query($this->db->getDb(), $query);

        if(mysqli_num_rows($result) > 0){
            mysqli_close($this->db->getDb());
            return true;
        }

        mysqli_close($this->db->getDb());
        return false;
    }

    public function createNewRegisterUser($username, $password, $email){
        $query = "insert into users (username, password, email, created_at, updated_at) values ('$username', '$password', '$email', NOW(), NOW())";
        $inserted = mysqli_query($this->db->getDb(), $query);

        if($inserted == 1){
            $json['success'] = 1;
        }else{
            $json['success'] = 0;
        }

        mysqli_close($this->db->getDb());

        return $json;
    }

    public function loginUsers($username, $password){
        $json = array();
        $canUserLogin = $this->isLoginExist($username, $password);

        if($canUserLogin){
            $json['success'] = 1;
        }else{
            $json['success'] = 0;
        }
        return $json;
    }
}
?>

我的目录如下所示:

AndroidLogin
|index.php
|include
 |config.php
 |db.php
 |user.php

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

通常,将文件称为您在其中声明的类。在WAMP中,它通常会出现一些问题,我建议您在db.php中重命名DbConnect.php

答案 1 :(得分:-2)

在DbConnect中创建一个默认(空)构造函数,并创建一个可以回显某些东西的简单方法。尝试让新的DbConnect实例从User类调用该方法吗?