doudou5101
2016-10-01 11:34
浏览 45
已采纳

OOP-php - 无法从数据库中检索数据

I'm a new with OOP model in PHP. I has been trying to retrieve data from database but something related to private makes me stuck. This is my code.

<?php 

require ("UserData.php");

class Database{

    public function getUser($sql){

    include ("includes/connect.php");
        $statement = $conn->prepare ($sql);
        $statement->execute();

        while ($row = $statement->fetch()) {

            $dataSet[] = new UserData($row);
        }

        if (!empty($dataSet)) {
            return $dataSet;

        }else{
            die();
        }
    }
}
?>

the second file

<?php 

class UserData
{
    private $user_id, $phone,$name,$address;

    public function _construct($dbrow){ 

        $this->user_id = $dbrow['user_id'];
        $this->name = $dbrow['name'];
        $this->phone = $dbrow['phone'];
        $this->address = $dbrow['address'];
    }
    public function getUserId(){
        return $this->user_id;
    }
    public function getUserName(){
        return $this->name;
    }
    public function getUserPhone(){
        return $this->phone;
    }
    public function getUserAddress(){
        return $this->address;
    }
}
?>

and the last one

<?php require ("Database.php"); ?>

<html>
<head>
    <title>OOP</title>
</head>
<body>
    <?php 
    include("includes/connect.php");

    $db = new Database();

    $dataSet = $db -> getUser ("SELECT * from user");


    if ($dataSet) {

        foreach ($dataSet as $data) {   
            echo "<p>";
            echo "ID" .$data->getUserId()."<br />";
            echo "Name" .$data->getUserName()."<br />";
            echo "Phone" .$data->getUserPhone()."<br />";
            echo "Address" .$data->getUserAddress()."<br />";
            echo "</p>";
        }

    }else{
        echo "no result found";
    }
    ?>
</body>
</html>

Well, I tried to var_dump the dataSet but the error shows up.

array(2) { [0]=> object(UserData)#5 (4) { ["user_id":"UserData":private]=> NULL ["phone":"UserData":private]=> NULL ["name":"UserData":private]=> NULL ["address":"UserData":private]=> NULL } [1]=> object(UserData)#6 (4) { ["user_id":"UserData":private]=> NULL ["phone":"UserData":private]=> NULL ["name":"UserData":private]=> NULL ["address":"UserData":private]=> NULL } }

So can anyone show me which spots make the code dump?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsbiw2911188 2016-10-01 11:55
    已采纳

    Everything is okay except this line,

    public function _construct($dbrow){ ...
                    ^^^^^^^^^^ it should be double underscore, not single
    

    Your constructor method in UserData class is wrong. It should be,

    public function __construct($dbrow){ ...
    
    打赏 评论
  • douzi8916 2016-10-01 11:43

    In PHP 5.6.0+, you can use the magic mathod __debugInfo()

    class UserData
    {
        public function __debugInfo() {
            return ['user_id' => $this->user_id, 'phone' => $this->phone, 'name' => $this->name, 'address' => $this->address];
        }
    }
    

    or makes variables public, which is not intend when you have methods like getUserId

    or create method to dump like:

    class UserData
    {
        public function dump() {
            var_dump($this);
        }
    }
    

    btw in class Database method getUser you include all the time when method is called, quick fix: use require_once instead of include, better will be to call include/require/require_once outside of class

    打赏 评论

相关推荐 更多相似问题