gpt4 book ai didi

找不到包含文件中的 Php 类

转载 作者:行者123 更新时间:2023-11-30 23:45:26 25 4
gpt4 key购买 nike

我试图遵循这个 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

我错过了什么吗?

最佳答案

通常,调用文件就像您在其中声明的类一样。在 WAMP 中通常会出现一些问题,我建议您在 DbConnect.php

中重命名 db.php

关于找不到包含文件中的 Php 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41466488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com