简体   繁体   English

如何修复致命错误-在php中找不到类'User'

[英]how to fix fatal error - Class 'User' not found in php

This is user class. 这是用户类。

<?php
class User {
    /* 
     *public static function get_all_users()
     */
    public function get_all_users() {
        global $link;
        $result_set = $link->query("SELECT * FROM `user`");
        return $result_set;
    }
}

Above class is user class i made object but cannot access. 上面的类是用户类,但我无法创建对象。 Display all use page of html: 显示html的所有使用页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252">
<title>Admin Panel ::: </title>
</head>
<body>
    <h3>Users :</h3>
    <table border="1">
        <tr>
            <th>S.N</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Username</th>
            <th>Password</th>
            <th>E-mail</th>
            <th>Contact</th>
            <th colspan="2">Option</th>
        </tr>
        <?php 
            $user = new User();
            $result_set = $user->get_all_users();
            /* 
             *$result_set = User :: get_all_users(); - accessing user class using static keyword.
             */
            $i = 1;
            while ($row = mysqli_fetch_assoc($result_set)) {
        ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['first_name']; ?></td>
            <td><?php echo $row['last_name']; ?></td>
            <td><?php echo $row['username']; ?></td>
            <td><?php echo $row['password']; ?></td>
            <td><?php echo $row['email']; ?></td>
            <td><?php echo $row['Contact']; ?></td>
            <td><a href="">Update</a></td>
            <td><a href="">Delete</a></td>
        </tr>                                           
        <?php 
                $i++;
            }
        ?>
    </table>
</body>
</html>

I can not access user class when I create object of user class and user class is inside includes folder and below HTML file is inside admin folder or admin ->viewuser.php and admin->includes->user.php it gives error. 当我创建用户类的对象并且用户类在include文件夹内并且HTML文件在admin文件夹或admin ->viewuser.phpadmin->includes->user.php之内时,无法访问用户类。

You need to include that User class inside the viewuser.php script ! 您需要在viewuser.php脚本中包含该User类!

include_once("includes/user.php");

I am not sure organizing your folders like that is the best way to do but it should work. 我不确定像这样整理文件夹是最好的方法,但是应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM