note 56044 added to function.mysql-fetch-object
| From: | trithaithus at tibiahumor dot net | Date: | Sun, 21 Aug 2005 20:56:00 +0000 |
| Subject: | note 56044 added to function.mysql-fetch-object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94002@lists.php.net to get a copy of this message | ||
This method offers a nice way to fetch objects from databases. As Federico at Pomi dot net mentioned
it doesn't work native as the type of the object fetched isn't the right one, but with a
small typecast it works flawlessly.
<?php
function ClassTypeCast(&$obj,$class_type){
if(class_exists($class_type)){
$obj = unserialize(preg_replace("/^O:[0-9]+:\\"[^\"]+\\":/i",
"O:".strlen($class_type).":\\"".$class_type."\\":",
serialize($obj)));
}
}
class Foo
{
var $foo;
var $bar;
function get_from_db()
{
mysql_connect();
mysql_select_db();
$res = mysql_query("SELECT foo,bar from my_table");
$fetched_object = mysql_fetch_object($res);
ClassTypeCast($fetched_object,"Foo");
$this = $fetched_object;
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-object.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56044
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56044&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56044&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56044&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56044&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56044&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56044&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56044&report=yes
Search -- http://master.php.net/manage/user-notes.php