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

时间:2016-10-01 11:34:03

标签: php oop

我是PHP中使用OOP模型的新手。我一直在尝试从数据库中检索数据,但与私人相关的东西让我陷入困境。 这是我的代码。

<?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();
        }
    }
}
?>

第二个文件

<?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;
    }
}
?>

和最后一个

<?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>

好吧,我尝试var_dump dataSet,但错误显示出来。

  

array(2){[0] =&gt; object(UserData)#5(4){   [&#34; USER_ID&#34;:&#34;的UserData&#34;:私人] =&GT; NULL [&#34; phone&#34;:&#34; UserData&#34;:private] =&gt;   NULL [&#34; name&#34;:&#34; UserData&#34;:private] =&gt;空值   [&#34;地址&#34;:&#34;的UserData&#34;:私人] =&GT; NULL} [1] =&gt; object(UserData)#6(4){   [&#34; USER_ID&#34;:&#34;的UserData&#34;:私人] =&GT; NULL [&#34; phone&#34;:&#34; UserData&#34;:private] =&gt;   NULL [&#34; name&#34;:&#34; UserData&#34;:private] =&gt;空值   [&#34;地址&#34;:&#34;的UserData&#34;:私人] =&GT; NULL}}

那么有人能告诉我哪些地方会使代码转储?

2 个答案:

答案 0 :(得分:3)

除了这一行,一切都没问题,

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

UserData类中的构造函数方法是错误的。它应该是,

public function __construct($dbrow){ ...

答案 1 :(得分:0)

在PHP 5.6.0+中,您可以使用magic mathod __debugInfo()

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

或使变量公开,当你有像getUserId

这样的方法时,这是不可取的

或创建转储方法,如:

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

btw在课程Database方法getUser中,您一直包括调用方法的时间,快速修复:使用require_once代替include,最好是调用{课堂外的{1}} / include / require