I'm trying to make a facebook style news feed on my webpage that combines user input from multiple tables into a feed.
Would the best way to approach this be:
(1) Using mySQL Join Queries
(2) Using mySQL single queries from multiple tables into an Array.
(3) Another way?
Thanks.
Edit:
The structure of my Databases are as follows:
1 User (Table: User)
Mutliple Blogs per user (Table: Blog)
Mutliple Content Upload per user (Table: Content)
What I am trying to achieve is a query that will collect the defined users ID's blog post and content uploads and display them in chronological order.
Thanks for the replies!
This is difficult to answer without knowing anything about your database schema (consider editing your question to add some details), but you may wish to consider creating a View that you could then retrieve and present your news feed from with a single, simple query.
If there is going to be a lot of data it is good to have a separate table that holds your "feed". That way, you don't have a lot of overhead each time you do your query.
Related
I am using Yii framework and I have a Post that has many comments, I need to get a list of posts, and each post should hold the latest 5 comments with it.
First Option: Is to make a join between posts and comments table and in the PHP code I should normalize the posts list to remove the duplicates and put the commets inside each related post.
Second Option: Is to load the posts list, then in PHP code iterate over these posts list and load the comments for each post using a separate sql hit.
Which one has the best performance and is there any better way to do it?
You should never have incremental hits on your database because of the data. Therefore, the first option would be the wisest one. If you do a join and then filter away the stuff you do not need, your program will be a lot faster than if you do one more database lookup for each row the previous query returned.
For more information, have a look on lazy and eager loading here: http://www.yiiframework.com/forum/index.php/topic/34412-eager-loading-vs-lazy-loading/
I am developing a site where users choose their city (from a long list), an activity (again a long list) and then a level of expertise. After that, the site shows them local events based upon their choices. I have over 100 POST variables (and more being added). How can this be done in MySQL?
You only have a few variables (city, activity, expertise). Use a session.
Here is a link to sessions in PHP (sessions)
Yes, you have the right idea. create a table and store each variable you would like to save.
post whatever variables from whatever scripting language of your choice, and the database statement would look something like this:
'INSERT INTO `your_table_name` (`field_1`,`field_2`) VALUES ('$post1','$post2');
you can add more fields as your information grows. That would be the best way to start getting into mysql. Of course this answer is the simplest solution. I recommend you read up on all fun and much more complex things you can do with mysql.
More efficiently I would do it like this:
categorize / classify your post variables and create multiple tables grouping similar things or data from a specific page. Create an auto increment primary id for your rows on the master table of the group, and then join your tables as needed. That's a little more advanced, so I suggest you add your few fields that you currently are using to a table now, and when you need to start adding more, looking these topics:
Normalization:
http://en.wikipedia.org/wiki/Database_normalization
Primary Keys, Joins, Stored Procedures, and the list goes on.
Im programming my own simple blog with comments in php and mysql. I have one database, and I have one table for posts, (called posts) and for each post I make I manually create a new table called comments1,comments2,comments3, etc. Each blog post has an id and when I retrieve the comments for a post I use a query like:
SELECT * FROM `comments".$id."`"
When I add user text to a comment I use:
htmlspecialchars(mysql_real_escape_string($_POST['name']));
Is this structure ok or is there a better way I'm missing?
Also, would creating a different database for comments be a better practice than grouping it with the posts database? Does having 2 dbconnect functions in one file slow down performance by a lot? And one last worry: how do i make absolutely sure that my php files aren't served to the user as plaintext, because then they could see db login info and such.
Thanks for the help.
No, this is TERRIBLE structure.
Make one comments table and store all comments in there, along with corresponding post_id.
Add an index on post_id and you'll be able to quickly get all comments for a given post.
I am working on a small project where a client would like to have a custom comment system to be shared within the internal network of there company. The logic is something like Google+, Facebook (other?) Where a user will make a Post and have the ability to choose people to share it with where the default (none) will go to everyone in that persons list.
My question is what is the best way to build up a table to store posts where it could have all or select people as the able viewers of said post. I guess my biggest issue is wrapping my head around the logic of it at the moment. Do I have multiple rows per post each with an id of the user(s) able to see said post, should I have a column on a single row for the post where I store an array or object of people able to view the post, I am open to suggestions. I haven't started working on it as of yet. So I am ultimately looking for advice on a good way to build the table that would support sound query logic, that won't cost me over head on either multiple queries or multiple rows I don't need. Don't want to begin without figuring something out as I don't want to box myself into something that will be harder to back out of in the long run.
What you are proposing is a one-to-many relationship. There is a ton of information about db relationships on the internet. Each Post could have Many people that would be allowed to use it. So you would have a posts table and a users table and a users_post table. The users post table would contain a post_id and a user_id. You would then have to check if the user could view the post through this relationship.
You could also put the users in groups, which would simplify this.
You should never store multiple values in an array in one column of the db.
Im making an application where users are able to comment on a lot of things, like blog posts, uploaded songs, pictures and so on.
Is it better to store ALL comments in one table where you have a column that points to what the comment was posted, e.g. blog, picture etc.
Or is it better to store them in separate tables, like "blogcomments" table, "picturecomments" table etc? Say for a site with 10000 plus users?
Thanks
If all comments have the same data being stored (e.g. comment content, user who posted the comment, etc), then it would make the post sense to keep them all in a single table. If they have different formats, then put them in separate tables.
In my opinion the first approach is the better one. Infact, this way, you can add (or remove) type(s) of comment (I mean post comment, blog comment and so on) without adding or deleting every time a table. I think this it is a more scalable solution.