Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) now 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!!!

like image 259
Abhishek Avatar asked Dec 07 '22 04:12

Abhishek


2 Answers

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.

like image 112
Pascal MARTIN Avatar answered Dec 09 '22 18:12

Pascal MARTIN


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.

like image 45
VolkerK Avatar answered Dec 09 '22 16:12

VolkerK