简体   繁体   English

如何使用php类函数将对象数据存储在mysql db中?

[英]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 我在DB中创建了一个名为“member”的表,其中包含字段“name”,“college”,“email”,“zid”,现在我创建了一个类(在php中)

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(); 现在在索引页面我将这些值作为用户的输入使用html表单(由JS验证)现在在表单的操作页面我想创建对象as obj = new member(); then calling the class function as obj->autherise($_post['zid']); 然后将类函数调用为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. 我希望autherise和adduser函数的定义如autherise检查zid的唯一性和调用adduser与剩余的post变量并将它们存储到object然后在一个查询中将整个对象添加到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 insert into member(name,email,college,zid) values('$name,$email,$college,$zid')我想直接输入obj到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 – ie 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. 这可以在PHP中使用serialize函数完成 - 并且将字符串反序列化为对象,您将使用unserialize函数。


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. 另一个解决方案是将对象序列化为JSON格式 - 这很好用的是JSON可以直接从Javascript和许多不同的编程语言中使用,使用正确的库。

For JSON, see json_encode and json_decode 对于JSON,请参阅json_encodejson_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; 作为旁注:请注意,如果将数据存储为序列化字符串,则在SQL中操作它们将更加困难 :例如,您将无法使用简单的SQL查询更新它们; 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. 这意味着您将始终必须从数据库中获取数据,使用PHP操作它们,然后将它们发送回数据库 - 根据您的需要,这可能并不总是一个好主意。

I'm not sure what you're asking for. 我不确定你要求的是什么。 But maybe...just maybe you're looking for an object relational mapper (orm) , like eg doctrine . 但也许......也许你正在寻找一个对象关系映射器(orm) ,比如教条

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. 如果您希望将对象存储在数据库中,可以将对象serialize()json_encode()转换为String,然后将其存储在表中的字段中。

When you are ready to use it again you can unserialize() or json_decode() the String again. 当您准备再次使用它时,您可以再次unserialize()json_decode()字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM