chaiwei Posted December 8, 2009 Share Posted December 8, 2009 Hi, I have a class, example bear, class bear { private $name; private $age; function __construct($name , $age){ $this->name = $name; $this->age = $age; } function save(){ sql = 'INSERT INTO xxx'; } } $daddyBear = new bear('daddybear' , '1'); $daddyBear->save(); If I have a bear table, example id -> (int) auto increment name -> (varchar) age -> (int) should I add a object? ( used to save the bear class so when the time I fetch the data from database I don't have to assign again the name) example: class bear { private $name; private $age; function __construct($name , $age){ $this->name = $name; $this->age = $age; } function save($class){ sql = 'INSERT INTO {bear}( name, age, object) VALUES( "'.$this->name.'" , '.$this->age.' , "'.$class.'" )'; } } $daddyBear = new bear('daddybear' , '1'); $daddyBear->save(serialize($daddyBear)); $sql = 'SELECT * FROM bear'; while($xxx = mysql_fetch_array(xxx)){ $bear[] = unserialize($xxx['object']); } echo $bear[0]->name; Wouldn't It be easier? so I can use all the attribute again without have to set the attribute. Is there any problem with my idea that I might miss out? Please advice. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/184353-serialize-the-object-and-save-in-database/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2009 Share Posted December 8, 2009 Just use mysql_fetch_object and specify the class_name you want to create an instance of - object mysql_fetch_object ( resource $result [, string $class_name [, array $params ]] ) Quote Link to comment https://forums.phpfreaks.com/topic/184353-serialize-the-object-and-save-in-database/#findComment-973245 Share on other sites More sharing options...
chaiwei Posted December 8, 2009 Author Share Posted December 8, 2009 oh... okok.. thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/184353-serialize-the-object-and-save-in-database/#findComment-973265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.