简体   繁体   English

显示数据库php oop中的所有用户

[英]displaying all the users from the database php oop

I'm trying to display all users from my db using php oop. 我正在尝试使用php oop显示我的数据库中的所有用户。 I really don't know how to include the user.php into connectionFile.php or something that might work.my code: user.php: 我真的不知道如何将user.php包含到connectionFile.php或可能起作用的东西中。我的代码:user.php:

    public class User{
    protected $firstname;
    protected $lastname;
    protected $email;
    protected $password;
    protected $age;
    protected $role;
    protected $file;
    protected $db;

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

    public function __construct($firstname, $lastname, $email){
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->email = $email;
    }

    public function getFirstname(){
        return $this->firstname;
    }
    public function getLastname(){
        return $this->lastname;
    }
    public function getEmail(){
        return $this->email;
    }

    public function setFirstname($firstname){
        $this->firstname = $firstname;
    }
    public function setLastname($lastname){
        $this->lastname = $lastname;
    }
    public function setEmail($email){
        $this->email = $email;
    }
    public function getAllUsers(){
        try{
            $stmt = $this->db->prepare("select * from user");
            $stmt->execute();
            return $stmt;

        }catch(PDOException $e){
            echo $e->getMessage();
        }
    }
   }

My connectionFile.php: 我的connectionFile.php:

require_once ("user.php");
// session_start();
$servername = "localhost";
$username = "root";
$password = "root";
$database = "first_project";
try {
    $conn = new PDO("mysql:host=$servername;dbname=first_project", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   // echo "Connected successfully"; 
 }
 catch(PDOException $e){
    echo "Connection failed: " . $e->getMessage();
 }

and my displayResult.php(I also have a page html with the form): 和我的displayResult.php(我还有一个表单形式的html页面):

 <?php
 require_once("connectionFile.php");
 //include_once 'user.php';
 if(isset($_POST['button'])){
    //$user = new User();
    echo "skksksks";
 }
 else{
    echo "v";
 }

?>

the button is working, I want in the button post to call the function getAllusers from user class but I really don't know how because every time I try to include that file anywhere it gives me page not found. 该按钮正常工作,我想在按钮栏中从用户类调用函数getAllusers,但我真的不知道如何,因为每次尝试在该文件所在的任何地方都找不到该文件时,就会发现该文件。

You can create the object of your class to access the methods. 您可以创建类的对象来访问方法。

require 'user.php';
$obj=new User($db_con);
$obj->setFirstName('test');

and so on... 等等...

And please make sure can you overload constructor in php... I do not think so.. 并且请确保您可以在php中重载构造函数...我不这样认为..

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

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