View Single Post
04-20-2011, 08:07 PM
#2
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

Relational databases are a fairly large concept. There are numerous tutorials online that cover a lot more than I'd be able to type up here. But in essence, relational databases are databases that have relations to other ones. So instead of storing the data in a CSV format they each have their own row in a separate table and they are linked together. This is faster, less error prone, more flexible and overall a much better practice. Again, there are extensive tutorials that you can find on Google for this subject.

Once you understand relational databases queries get more efficient (although a little harder to write). The script you have there could be done in one single query with a properly designed database. To give you an example here is how I would do the above:

table users:
user_id int(9) primary_key, auto_incriment
user_name varchar(255)
ect...

table places:
place_id int(9) primary_key, auto_incriment
place_name

table ratings
rating_id int(9) primary_key, auto_incriment
rating_place_id int(9)
rating_user_id int (9)
rating_score int(1) //0=thumbs down, 1=thumbs up

This would get ratings by place:
SELECT COUNT(rating_id), place_name FROM places LEFT JOIN ratings ON rating_place_id=place_id GROUP BY place_name
Example output:
10 | Chicago
8 | New York
15 | Paris

This would get ratings for a particular user:
SELECT rating_score, place_name FROM ratings LEFT JOIN places ON rating_place_id=place_id WHERE ratings_user_id=[USER_ID]
Example output:
1 | Chicago
0 | Paris
1 | New York

Thanked by 2 users:
Artashes (04-25-2011), Tomos (04-22-2011)