4

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=>$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

3
  • heh? whats the issue? whats does $object->row contain at the moment? Commented May 20, 2011 at 15:47
  • sorry $object->row is a member of the class that stores selected row from the database Commented May 20, 2011 at 15:49
  • So $object->row is a member that contains an array of assoc. arrays, which you want to be turned into an object? Thing is, no matter what you do you'll deal with arrays, so I don't see why you're trying to convert it all to an object. Commented May 20, 2011 at 15:49

4 Answers 4

6

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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! :)

2 Comments

Thanks DS, think this would answer it.
Thank you for your vote :) Try these libs, at least Doctrine: is awesome!
0

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

4 Comments

Well its OO php, Users would be the table so this is a class named the same, you instantiate it by $users = new Users(); The db is simple a top class containing methods to communicate with the Database. The DB_SELECT pretty much returns the result from a mysql_query($sql, $connection);
@yes123, what about useless? Its fantastic to have your tables in separate classes.
@yes123, well it's a strongly typed class (you probably know about them) in this case i just skipped the datatyping, and so what if I call a parent method DB_SELECT, thats one line dude, chill.
@yes123, declaring to someone that their "code is just awful" is incredibly rude. You need to take some time to read the FAQ; in particular, Be Nice.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.