Objects in and out of a DB
| From: | Nigel Gilbert | Date: | Thu, 09 Aug 2001 16:23:16 +0000 |
| Subject: | Objects in and out of a DB | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-11192@lists.php.net to get a copy of this message | ||
This is probably a FAQ, but the on-line manual is rather confusing and I can't get it to work!
The problem: I have a fairly complicated set of objects, all interconnected through the instance variables of one object storing the value of another and so on. Other instance variables hold strings. I want to store the whole thing in a PostgreSQL database and then later recreate the entire hierarchy with everything set up as before.
This is what I am using ($zurich is an instance of the Class Zurich):
$s = addslashes(serialize $zurich));
$result = pg_exec($db, "UPDATE zurtable SET serializedobject = '$s'
WHERE name='Zurich'");[error checking omitted] This puts something sensible into the database; the first few characters of what is retrieved using the database front end psql looks like this: O:6:\"zurich\":10:{s:7:\"players\";a:7:{s:14:\"water_supplier\";O:14:\"water_supplier\":3:{s:4:\"name\";s:14:\"water_supplier\";s:10:\"reservoirs\";a:5:{i:0;O:9:\"reservoir\":5:{s:13:\"building_cost\";i:2500;s:11:\"repair_cost\";i:2000;s:14:\"maintenance_pa\";i:50;s:7:\"max_age\";i:10;s:3:\"age\";i:4;}i:1;O:9:\"reservoir\":5:{s:13:\"building_cost\";i:2500;s:11:\"repair_cost\";i:2000;s:14:\"maintenance_pa\";i:50;s:7:\"max_age\";i:10;s:3:\"age\";i:1;}i:2;O:9:\"reservoir\":5:{s:
[many lines omitted]itical_popularity\";i:5;s:19:\"supplier_popularity\";i:5;s:13:\"env_awareness\";i:5;s:5:\"clock\";i:997373335;s:4:\"time\";s:31:\"Thu, 9 Aug 2001 17:08:55 +0100\";} However, when I come to extract the field from the database using: $result = pg_exec($db, "SELECT serializedobject FROM zurtable WHERE name='$Zurich'"); $row = pg_fetch_array($result,0); $new_s = $row['serializedobject']; the output ($new_s) is null. What am I doing wrong?