note 32088 added to function.mysql-fetch-object

From: Date: Thu, 15 May 2003 23:44:18 +0000
Subject: note 32088 added to function.mysql-fetch-object
Groups: php.notes 
Request: Send a blank email to php-notes+get-48052@lists.php.net to get a copy of this message
Here is a wrapper that will allow specifying a class name. function &db_fetch_object($set,$className) { /* Start by getting the usual array */ $row = mysql_fetch_assoc($set); if ($row === null) return null; /* Create the object */ $obj =& new $className(); /* Explode the array and set the objects's instance data */ foreach($row as $key => $value) { $obj->{$key} = $value; } return $obj; } class CPerson { function getFullName() { return $this->fname . ' ' . $this->lname; } } $set = mysql_query('SELECT fname,lname FROM person'); while($person =& db_fetch_object($set,'CPerson')) { echo $person->getFullName(); } -- http://www.php.net/manual/en/function.mysql-fetch-object.php http://master.php.net/manage/user-notes.php?action=edit+32088 http://master.php.net/manage/user-notes.php?action=delete+32088 http://master.php.net/manage/user-notes.php?action=reject+32088

« previous php.notes (#48052) next »