开发者

mysql and php how to store the information

I'm making an application where users can sign up for courses.One user can signup for multiple courses, and in the end the data that I am interested in looks like this.

 array(
  0 => array(
       0             => 12, // 0 is nothing 12 is the course id which i use to refference
       'date_joined' => 1301123384 // when the user joined the course
       ),
  1 => array(
       0             => 52,开发者_运维百科 // the same as above
       'date_joined' => 1301123384 
       )
 )  

I also need the keys of the main array to determine the order in which the user joined. To store it i serialize it into a string and save it in the database.

Is this a good method ?

Could it be done differently ? Better ?

I don't need a mysql query i need to know if this information could be stored otherwise than an array turned into a string


No that's definitely not how you should be storing it. You need to normalize your DB design so you can use querying to get your job done. If you serialize it, you won't be able to query it (in the conventional way, at least). The following is a better schema

Students : sid | s_name | more | person | data | created |
Courses : cid | course_no | c_name | some | more | info | created
Students_Courses : id | sid | c_id | created

Breaking it down

You have a Students table with student information and a Courses table with course information. They you have a join table with the SID and the CID in it to make a unique key. This will let you query the courses a student is part of and all the students that subscribe for a course as well. Since you have a created column, you can use it to ORDER BY so you know which courses came first.


Why not use a table that has three columns
1)user_id
2)subject_id
3)date_joined
and store it there? If you serialize and store the array you will not be able to query easily. For example - to get the dates people signed up for a certain subject.


Serializing and storing the data structure as a String is certainly a very bad way of doing this.

You should use relational tables to store this data.

You could, for ex., create a course_attendees table that has 3 columns - user_id, course_id and join_date. Join_date can e used to track the order in which the courses were subscribed.

This will help you query the tables more efficiently rather than writing code to serialize and deserialize the string to your data structures.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜