I have a table in database and there is column which contains multiple questions separated by commas(hockey,cooking,movies).
My question is that right now I'm firing a select query which is like this:-
$query="$sql="SELECT * from user where hobbies='$form_id'";
Actually in $form_id I'm passing the value say(games), now I want to display all the rows in the database which contains 'games'in them. Remember i have to compare this value with multiple values in the columns in that particulars of the database.
I'm using corephp with mysql as database. I really don't know how to do this please help me out.
SELECT * FROM user WHERE hobbies LIKE '%$form_id%';
but you should change your database schema like:
users(id, name, firstname, ...)
hobbies(id, name)
user_hobbies(id_user, id_hobby)
In reaction to John Conde's remark, the problem here is lying to your data model.
If a person can have several hobbies, you shouldn't store those hobbies in a field of the "person" table. Long story short, you should have 3 tables :
"Person" Table => Everything related to the person, and unique : name, birthdate, etc... NO HOBBIES !
"Hobbies" table => Everything related to the hobbies. The most simple form would be 2 fields : id & hobbyname. You could imagine adding more details in other fields, like is it an indoor or outdoor hobby, etc...
"PersonHobbies" table, used to link both : In that one, you'll probably have only 2 fields, 1 for the person id, one for the hobby id. Then if a person has 3 hobbies, you just write 3 records to that table with the same user id and a different hobby id
There are many advantages to this normalization. the biggest one is that it makes your queries easier to select but also update hobbies of a user. It also makes it easier to count how many hobbies each person has and so on. so, you SHOULD ALWAYS normalize your data, it doesn't take much more time when creating database and waves you a lot of time afterwards
Related
I am uncertain of the best way to store multiple select options into mysql. Initially, I created a comma separated list of selected values. I have read that this is bad programming practice and also had a difficult time trying to figure out how to search the three columns in my db with the comma separated varchar value (eg: 1,5,17). It seems the best practice would be to create a 3rd table that would insert 3 records for the example above. This table would be linked via an id.
Example:
Table 1 (age levels)has fields id & age level (baby,teenager,adult)
Table 2 has classes id & class name
Table 3 is to store the multiple select options of WHO can attend each class. I guess the fields would be id & classID & ageLevelID
My questions are as follows:
1. how do I efficiently insert this data into table 2 and table 3?
2. how do I pull it all together to search for a person who is looking for a class that allows for just babies and teenagers, for example?
Relating 2 tables is easy, relating 3 tables is a conceptual struggle. Also, am I correct that it is asking for trouble to store comma delimited values in one field? Thank you.
Table agelevel (ageid (unique), agelevel)
Table classes (classid (unique), classname)
Table classesinfo (classid, fieldname,fieldvalue)
With that you could have any 'fieldname' such as agelevel, detailed description, hours, etc and then the value of that field in fieldvalue. This allows your classes to any an infinite number variables attached to a single classid.
I am new and trying to make a simple website for a project. I have to pages, add and view hobbies. User needs to be able to add 1 or more hobbies and store it in a database and to retrieve it in another page, i have listed what i plan to do below.
How do i make it such that I am able to store multiple hobbies for users in the database?
Using MySQL, HTML, PHP, Dreamweaver
Assume: User has been created, user has been logged in and all user related database coding has been done. More than 1 user exists, all users are to have a few hobbies each.
Database
User table: UserId, Username, HobbyId
Hobby table: HobbyId, HobbyName, HobbyDaysAWeek
1 user can have 0 or more Hobbies.
Add hobby page
dropdownlist (binds list of hobbies from hobby table)
textbox (for user to enter how many days a week he spends engaging in that hobby)
add button (displays value in dropdownlist and textbox in a table)
table (Hobby and daysAWeek will be shown here, with a delete icon to delete it)
submit button(saves hobby & days a week & username into database)
View hobby page
Displays user's hobby & days a week in a table.
Thanks for helping! And please provide simple and bare minimum coding as i am new!
Well, nobody posted an answer but my question was answered in the comments so..
Start by adding a 3rd table with UserId and HobbyId – Dagon
You're storing the relationship between, not a relationship. So think of it like you have two things and you want to remember that they relate. Thus, a table with two columns, one with an identifier of one part, the other with identifier (ID) of the other part. Pull them together with the data about each part, you have information. Hence, you've stored the relationship. It's like metadata, really. It's usually pretty efficient. - Jared Farrish
Thanks guys!
I use MySQL and trying to write a PHP script for my school project.
There is one table named lessons contains this columns:
-id
-lessonid.
-studentid
I also have two different tables for notes and announcements
announcements and notes tables contains these columns:
-id
-lessonid
-content
-createdtime
I need to order both announcements and notes from latest to oldest by createdtime but also need to show all lessons a student takes.
For example: A students takes maths and physics lessons. I need to display him/her both notes and announcements for both of physics and maths and all items should be ordered by date. (like a timeline.) And of course I will not show him/her the notes and announcements for chemistry lesson. Also it will be good if I can say it is note or announcement on the list.
Can you help me to write SQL and PHP code for that?
Thanks.
EDIT: This is where I have stuck:
I have combined two tables and ordered them by date. But can't combine them with the lessons a student take.
SELECT title, created, lessonid FROM (SELECT title, created, lessonid FROM notes UNION SELECT title, created, lessonid FROM announcements) as a ORDER BY created DESC
First of all, thanks for letting us know that this is for a school project - therefore I won't give you the answer. If it is in the project then your teacher should have given you the concepts to come up with a solution.
Your question is well put together and I can see how to solve it but ... It's your project so you need to have a crack at it and post what you come up with.
I will give you some hints to get you started.
You need a query to combine the announcements and notes table. Then you need to group the data by the lesson and join that to the students. This is all basic SQL.
Good luck. Post what you come up with.
I'll also, follow fellow posters advice, and not do the legwork for you. but won't let you go empty handed, so will give you the concept.
there is a thing called third normal form, we decide how many tables according to that concept, so if its a big database then separate table for first name and separate for last name, as many people share those among themselves, so saves space and redundancy etc. so one table for person has personid as primary, and has lastname foreign key to refer to last name table , we generally name it lastNameRef, similarly firstNameRef. so now, each person has lot of classes, and each class has lot of persons(students) in it. so this is a many-many relation - we create a allreference table to solve this many to many problem. so there is one table for classes which has class id as primary key, so now u create a all reference table which a recordId as primarykey, (just for namesake) and personRef(refers to personId in person table) and classref(refers to classId in class table) if one person has two classes, another entry with same personId but different class Id, at the end, you can query the name of person from person table, and name of class from class table and create join on their foreign keys but use all three tables, result is (JOHN MATH, JOHN SCIENCE) etc, same way you display all notes for john searching name in person table, and subject in class table,and notes in notes table
I have a database with employees. the columns are first name, last name, department, internal number etc..
As for today it is a database only for one organization but in future i want to add to this database employees from other relative organizations.
What is the right way to do it:
To add another field to the first table ?
To create another table with 3 fields: id, organization_name, employees ( where in this filed i would put comma separated values of id from first table) ?
if the second answer will be chosen what will happened when an update query will be executed simultaneously from different accounts to the same organization. For example: i will be adding a user with id 55 to organization 'Police' and at the same time another administrator will be adding to the same organization a user with id 65..
In this case is there a possibility of error or data-loss ???
If someone had this kind of problem before, i really would like to read about it..
Thank You..
If the organization is only a number to group the users, then I would suggest to put them into the employees table. However if you have more information about the organization (e.g. name, address .. ) then make a new table for the organization and save the primary key of the corresponding organization in the employees-table.
I'm having trouble figuring out the best way to store what information my users want to see. I apologize for not being able to find an easy way to explain my problem, but here goes (the fruits are for example's sake):
Let's say my users have an option to keep track of the following fruits in their fridge:
Apples, Oranges, Grapes, Pears
The database is already setup with a table called 'fruits', where Apples is id_fruit=1, Oranges is id_fruit=2, etc.
Like I said all users have the OPTION to keep track of these fruits, however... I currently can not figure out an easy way to store the users selection of WHICH fruits they want to track. If user 1 doesn't like Apples, he can just go into his settings, uncheck Apples and no longer have Apples show up in his dashboard.
The only way I can think to store this information is to create a table that contains for fields user_id, apples, orange, grapes, pears. And then storing the user_id and Yes/No or 0/1 for the fruits.
This causes major grief if I ever want to add more fruits, or whatever.
What is the best way to store this information in MySQL, using PHP. And how would I retrieve that info.
User logs in, check the DB for which fruits he wants to see, and display only those fruits he has chosen.
Any help would be greatly appreciated.
This is a classic has-and-belongs-to-many, or simply many-to-many, relationship.
Table `users`
=============
id
name
...
Table `fruits`
==============
id
name
...
Table `users_fruits`
====================
id
user_id
fruit_id
In users_fruits you store one row per user-fruit relationship: (user_id: 42, fruit_id: 3).
Create a table tracking the actual relation.
You have one user table, with user id's. You have one fruit table, with fruit id's.
Create a table with user-fruit-relations. Let it have three colums, id, user_id and fruit_id.
If user 1 tracks fruits 2,3 and 5, insert those three rows in the table. That is, three rows with user_id = 1 and the different fruit_id's for each row.