473,549 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Objects saving and restoring with MySQL

Gav
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?

--
Gav

Jan 11 '06 #1
7 3952
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($Datab ase){
// make some SQL with just the data fields you want saved
$sql = "update atable set password=$passw ord ..... where
name=$this->name";
$Database->ExecuteSQL($so meSQL);
}

OR
function SaveToDb($Datab ase){
$blob = serialise($this );
// now save blob in database keyed by name
$sql = "update atable set blobfield='$blo b' where name=$this->name";
$Database->ExecuteSQL($so meSQL);
}

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
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.co.uk>
Jan 11 '06 #2
NC
Gav wrote:

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?
By using serialize($your _object) and writing the result into a TEXT or
VARCHAR field.
Are there any tutorials that you can recommend on this subject?


The Manual is always a good place to start:

http://www.php.net/serialize

Cheers,
NC

Jan 11 '06 #3
> Are there any tutorials that you can recommend on this subject?
Wrote this when I was searching for any methods myself:

http://www.w-p.dds.nl/article/wtrframe.htm

Hope it is of any help.
Jan 11 '06 #4
NC wrote:
Gav wrote:

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?


By using serialize($your _object) and writing the result into a TEXT or
VARCHAR field.


I don't see how that's better than having separate database fields for
each object member. It makes it very difficult to efficiently query the
database based on different criteria, or to migrate to any language
besides PHP.

-- David

Jan 11 '06 #5
Following on from David Wahler's message. . .
NC wrote:
Gav wrote:
>
> 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?


By using serialize($your _object) and writing the result into a TEXT or
VARCHAR field.


I don't see how that's better than having separate database fields for
each object member. It makes it very difficult to efficiently query the
database based on different criteria, or to migrate to any language
besides PHP.

-- David

What you do is set up a table with fields something like
uID,uStatus,uNa me,uData
where data is blob/text.

You can select on some items of data but you have complete flexibility
in the blob when you serialise the object. Suppose for example the user
object contains a variable size array. Mapping that to table fields
would be a pain or setting up another table for 1 user to many array
elements complete overkill when the information isn't going to be used
in any other context. For example an array of passwords used in last
three months by this user to prevent immediate re-use.[1]

[1] Don't try this at home kids! Never store passwords.
--
PETER FOX Not the same since the porcelain business went down the pan
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.co.uk>
Jan 12 '06 #6
serialize() puts out binary data. VARCHAR and TEXT are made for character
data. use BLOB(), TINYBLOB, or MEDIUMBLOB.

"NC" <nc@iname.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Gav wrote:

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?


By using serialize($your _object) and writing the result into a TEXT or
VARCHAR field.
Are there any tutorials that you can recommend on this subject?


The Manual is always a good place to start:

http://www.php.net/serialize

Cheers,
NC

Feb 12 '06 #7
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?


1 - Serialize into a blob AND use some key fields as required is one
way. But users tend not to have really complex data footprints so write
a method for your user class SaveMe() which does the dirty deed field by
field.

2 - Or download this very thing from my web site
http://www.eminent.demon.co.uk/phplibrary.htm
[with added access permissions goodness]

--
PETER FOX Not the same since the cardboard box company folded
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.co.uk>
Feb 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3478
by: PeterF | last post by:
Hello, following problem: I use a mysql query string like $arg= " insert into ".$sometable; $arg.=" (id, col1) "; $arg.="values ( '$this->id', '$data' ) "; ^^^^^^ $data is a product of serialize($some_object)
7
7401
by: Adam Smith | last post by:
Hope this is the right news group!! I did a server upgrade and at the same time did a mysql update from 4.0.12 to Ver 12.22 Distrib 4.0.16 mach1# mysql --version mysql Ver 12.22 Distrib 4.0.16, for portbld-freebsd4.9 (i386) I attempted to restore the DB with
2
2353
by: Yair Sageev | last post by:
Greetings. I was running PHP Nuke on a Win2k box using Apache2Triad. Fortunately I backed up the drive using windows backup prior to the hard drive failure. Now I have installed Apache2Triad on another machine and wish to reimport the mySQL database that PHP Nuke relies upon. If anyone can tell me how to do this I would be very...
5
7247
by: Michael Albanese | last post by:
Can you save objects a (custom class) to Viewstate and then get them on a later page?? I have a custom class for an ASP.Net application that I would like to make available across several web pages. For example: page1.aspx accepts a users's name, age and gender, instantiated the new class and redirects to page2.aspx
2
3232
by: Raffi | last post by:
I use the --all-databases switch to backup my entire database. Sometimes there's a need to restore individual databases or tables form the backup file. What command should I use for this? Thanks, Raffi
1
3887
by: Brian Sadler | last post by:
Does anyone know where I can find information on how to setup my shell in unix/linux to allow me to restore a database from an sql dump... Example... I have a database on 83200.mysql.com (example) that I have a file mysql.sql (also just an example). The file was produced using an sqldump command from a web interface...
1
1625
by: Tom | last post by:
I have a large application; lots of forms, multiple dynamic DLLs, etc. I also have, in the appliation, a general 'Preferences' class object (which is in itself a separate DLL, and I just include a reference to it so I can instantiate it at the beginning) which stores all my user preferences. I serialize the data to and from an XML file,...
15
3719
by: Marcus Kwok | last post by:
How do you save the formatting options for a stream? In the program below, notice that when I define my operator<<() for the custom type, it permanently changes the output format of the stream: #include <iostream> #include <iomanip> struct Data { double d; };
16
7167
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. I've used the following code posted by Lyle Fairfield to accomplish the first step: Private Sub SaveObjectsAsText() path = CurrentProject.path & "\ObjectsAsText\" SaveDataAccessPagesAsText...
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.