OK, this isn't exactly a database question, but I figure that those that hang out in this forum are most likely to have tried what I'm attempting...
I have written a class that describes an object that I'd like to be able to read / write from a database.
In order to help keep the code where the I'm using the object clean, I want this object to have it's own built-in methods to read and write from MySQL.
I wrote two methods for the class, one called write_to_db(), and one called read_from_db(). The "write" function works perfectly. It serializes $this, and INSERTs it (along with a unique ID) into the database, or performs an UPDATE if that ID already exists.
Unfortuantely, my read_from_db() method falis to unserialize my data at all. The pertinent code looks like this:
function read_from_db($db_link,$id_number) {
$query = "SELECT * FROM some_table WHERE id='$id_number'";
$result = mysql_query($query,$link);
$data = mysql_fetch_assoc($result);
$this = unserialize($data['object']); //assuming that the 'object'
//column holds the serialized object
}
I'm sure it's the last line that is my problem. I'm figuring that I can't just redefine $this to whatever I want to from a method within the class, but there was no problem grabbing the data to serialize it in the first place by doing serialize($this).
Now please, somebody, tell me how bad(ly) I am doing this. 🙂