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){ ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块