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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看