PHP定义对象类型以保存MySQL表数据?

时间:2016-03-05 06:24:22

标签: php mysql

我基本上是尝试从MySql数据库中检索一行,并将行数据存储在一个Object中并将其传递给我的视图。我不需要数组,而是需要将其存储为名为“User”的自定义数据对象。

这是我到目前为止所做的:

public function GetUserData(){

    $this->load->database();
    $query = $this->db->query('SELECT * from eyebase.UserTable WHERE id=1');

    //I have already implemented an array. I need to replace this with an object
    $users= array();
    foreach ($query->result() as $row)
    {
           $users[] = $row;
    }

    //I need to replace the line below with the object I created, instead of the array
    return $users;
}

//This is the object I need to use
class User{
     public $id;
     public $name;
     public $description;
     public $picture;
}

很简单,只需将行的列值(例如id,Name,Description,Picture)存储在对象中并返回它,而不是将它们存储在我已经实现的数组中。我该怎么做?

0 个答案:

没有答案