views:

220

answers:

4

Hello! 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!!!

A: 

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.

Dan Millar
actually I am fine to understand the concept but problem in writing the code
Abhishek
<?phpinclude ('dbc.php');include ('class.php');$student = new member();$student->auth($_POST['uname'],$_POST['email'],$_POST['college'],$_POST['zid']);$student = addslashes(serialize($student));mysql_query("INSERT INTO member(member) VALUES('$student')") or die("Sorry");?> please check it!! :(
Abhishek
+2  A: 

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 sued 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.

Pascal MARTIN
as per your answer I need to make $obj=serialize($obj); then what would be sql query for that
Abhishek
Please reply I am unable to move forward
Abhishek
<?phpinclude ('dbc.php');include ('class.php');$student = new member();$student->auth($_POST['uname'],$_POST['email'],$_POST['college'],$_POST['zid']);$student = addslashes(serialize($student));mysql_query("INSERT INTO member(member) VALUES('$student')") or die("Sorry");?>please tell am i doing correct?
Abhishek
+2  A: 

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.

VolkerK
+1  A: 

Search around ORM model , Study it in detail ,you will find your answer

vinayrks