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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵