How to store the object data in mysql db using php class function?
I have created a table in DB with name "member" having field "name", "college", "email", "zid" now I have created a class(in php) like
class member
{
private $name,$college,$email,$zid;
private function adduser
{
//function definition
}
public function autherise($id)
{
//function definition
}
}
now at index page I am taking these value as input from user using html form(validated by JS) n开发者_如何学Cow at action page of form I want to create object as obj=new member(); then calling the class function as obj->autherise($_post['zid']); I want the defintion of autherise and adduser function like autherise check the uniqueness of zid and the calls adduser with remaining post variables and store them to object then add whole object in one query to DB.
I dont wan
insert into member(name,email,college,zid) values('$name,$email,$college,$zid')
I want to enter obj directly to the db
You can modify anything in functions
Thanks in Advance!!!
An "easy" solution to store a whole object somewhere, like in a database, is to serialize it – i.e. transform it to a string ; and, then, store that string in a single field in the database.
This can be done, in PHP, using the serialize
function – and to de-serialize the string to an object, you'll use the unserialize
function.
Another solution would be to serialize your object to the JSON format – nice thing with that is that JSON can be used directly from Javascript, and from lots of different programming languages, using the right libraries.
For JSON, see json_encode
and json_decode
As a sidenote : note that if you store your data as serialized strings, it will be much harder to manipulate them in SQL : you will not be able to update them using a simple SQL query, for instance; and searching will be hard too.
This means you'll always have to fetch your data from the database, manipulate them with PHP, and send them back to the database – which might not always be such a good idea, depending on your needs.
I'm not sure what you're asking for. But maybe...just maybe you're looking for an object relational mapper (orm) , like e.g. doctrine.
If you were looking to store an object in a database you could serialize()
or json_encode()
the object into a String and then store it in a field in the table.
When you are ready to use it again you can unserialize()
or json_decode()
the String again.
精彩评论