How to store the object data in mysql db using php class function?

16,019

Solution 1

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.

Solution 2

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.

Share:
16,019
Abhishek
Author by

Abhishek

Abhishek Kumar Srivastava PHP/Joomla,Social Media Application developer http://abhisheksrivastava.in http://webkul.com If you want to be updated Follow me @ www.twitter.com/abhishekkrsri

Updated on June 13, 2022

Comments

  • Abhishek
    Abhishek almost 2 years

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

  • Abhishek
    Abhishek about 14 years
    as per your answer I need to make $obj=serialize($obj); then what would be sql query for that
  • Abhishek
    Abhishek about 14 years
    Please reply I am unable to move forward
  • Abhishek
    Abhishek about 14 years
    actually I am fine to understand the concept but problem in writing the code
  • Abhishek
    Abhishek about 14 years
    <?php include ('dbc.php'); include ('class.php'); $student = new member(); $student->auth($_POST['uname'],$_POST['email'],$_POST['colle‌​ge'],$_POST['zid']); $student = addslashes(serialize($student)); mysql_query("INSERT INTO member(member) VALUES('$student')") or die("Sorry"); ?> please tell am i doing correct?
  • Abhishek
    Abhishek about 14 years
    <?php include ('dbc.php'); include ('class.php'); $student = new member(); $student->auth($_POST['uname'],$_POST['email'],$_POST['colle‌​ge'],$_POST['zid']); $student = addslashes(serialize($student)); mysql_query("INSERT INTO member(member) VALUES('$student')") or die("Sorry"); ?> please check it!! :(