开发者

PHP OOP: How to get database rows as objects?

开发者 https://www.devze.com 2023-03-07 19:52 出处:网络
I can do this when selecting a single row fine but cant quite get my head around doing this for multiple rows of data.

I can do this when selecting a single row fine but cant quite get my head around doing this for multiple rows of data.

For the single row I simply instantiate a new object that does a number of operations behind the scenes that bascially produces a row from the database as our object.

Example:

$object = new Classname($param);
foreach($object->row as $key=开发者_JAVA技巧>$value) {
    echo $key.":".$value."\n";
}

//output
id:1
firstname:steve
lastname:took
etc...

Any clever people here able to point me in the right direction please?

NOTE: just want to be able to create an object for each row rather than the one object with nested arrays

EDIT: sorry $object->row is a member of the class that stores selected row from the database


If I got you the answer is pretty simple mysql_fetch_object

Example:

while ($row = mysql_fetch_object($result)) {
    echo $row->user_id;
    echo $row->fullname;
}


Why don't you consider to use an ORM (Object Relational Mapper), like Doctrine or Propel? I prefer Doctrine: http://www.doctrine-project.org/ Enjoy! :)


Here is an example

class users extends db {

    public $users_id = 0;
    public $users_group = 0;
    public $users_firstname = 0;
    public $users_lastname = 0;

    public function open($id) {
        if (is_numeric($id)) {
            $sql = "SELECT * FROM users WHERE users_id = '$id'";
            $res = parent::DB_SELECT($sql);
            if (mysql_num_rows($res) <> 1) {
                return 0;
            }
        } else {
            $sql = "SELECT * FROM users " . $id;
            $res = parent::DB_SELECT($sql);
            if (mysql_num_rows($res) <= 0) {
                return 0;
            }
        }


        $data = array();
        while ($row = mysql_fetch_array($res)) {
            $this->users_id = $row['users_id'];
            $this->users_group = $row['users_group'];
            $this->users_firstname = $row['users_firstname'];
            $this->users_lastname = $row['users_lastname'];
            $data[] = (array) $this;
        }
        return $data;
    }

    public function setUsersId($users_id) { $this->users_id = addslashes($users_id); }
    public function getUsersId() { return stripslashes($this->users_id); }

    public function setUsersGroup($users_group) { $this->users_group = addslashes($users_group); }
    public function getUsersGroup() { return stripslashes($this->users_group); }

    public function setUsersFirstname($users_firstname) { $this->users_firstname = addslashes($users_firstname); }
    public function getUsersFirstname() { return stripslashes($this->users_firstname); }

    public function setUsersLastname($users_lastname) { $this->users_lastname = addslashes($users_lastname); }
    public function getUsersLastname() { return stripslashes($this->users_lastname); }
}


You could use MySQLi.

// Connect
$db = new mysqli('host', 'user', 'password', 'db');

// Query
$users = $db->query('SELECT * from users');

// Loop
while($user = $users->fetch_object()) {
    echo $users->field;
}

// Close
$users->close();
$db->close();

More info here: http://php.net/manual/en/book.mysqli.php

0

精彩评论

暂无评论...
验证码 换一张
取 消