|
Posted by Peter Fox on 01/11/06 11:44
Following on from Gav's message. . .
>If you had a class user with variables id, name, password. How would you
>save this object or its variable date to a MySQL database? And then if
>you had a dbase populated with id, name, password and you wanted to show
>a list of all of these how would you iterate through this and re
>populate the object variables?
>
>Are there any tutorials that you can recommend on this subject?
>
>
>
You would encapsulate the mechanism in the object as far as possible in
your environment. For example if your DB is [wrapped in] an object then
you might have the following as a method of myObject
function SaveToDb($Database){
// make some SQL with just the data fields you want saved
$sql = "update atable set password=$password ..... where
name=$this->name";
$Database->ExecuteSQL($someSQL);
}
OR
function SaveToDb($Database){
$blob = serialise($this);
// now save blob in database keyed by name
$sql = "update atable set blobfield='$blob' where name=$this->name";
$Database->ExecuteSQL($someSQL);
}
You will see that in the first example you would have the fields
explicit in the table but in the second everything but the key is
blobbed. The second approach has two advantages and one disadvantage
(but you can mingle)
+ If you change object design you don't need to change the database
+ You can store all sorts of object in the same table
- You can't SELECT on blobbed data
For iterating through a database results set or record see the docs it
is very straight forward EXCEPT for a gotcha in foreach where A COPY of
objects is worked on not the original. i.e. don't use foreach where
you'll be modifying data within it.
* The code above is off the top of my head and will have bugs.
--
PETER FOX Not the same since the bookshop idea was shelved
peterfox@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
[Back to original message]
|