With PHP & MYSQL, I need a scalable way to show a few comments for each post just like in facebook. For all the posts in my application I need to print the most recent comments beneath the post. The problem is that I dont want to put a SQL query in a loop and have to run 20 SQL queries per page. Is there one SQL query that I can use to get the 2 most recent comments for each post that I get from the posts table.
I believe you can retrieve all the comments using one query and then store them in an array. You can then iterate over the array to organize them.
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 have an application that categorizes links for a tools page. On this page I want to have multiple categories and each tool under those categories. As I am going to have 13 categories (with more as the tool gets used) I don't want to run a query for each category. I would prefer to run a single query to grab all the data.
Right now I have the page setup so that it displays each category with a drop down to view the tools. What I need to do is have it so that only the tools for that category show up. I'm having a problem working out how to go through all of that data from a single query.
Currently I am using a while statement to run through the results as an array.
while($tools = $result->fetch_assoc()){
if($tools['id'] = 1){
echo $tools['name'];
}
}
This is just a basic mockup of what I currently plan on doing. I can't help thinking however there is a better way to go about this. What is the best way to grab a big chunk of data from MySQL and then break it out into categories with PHP? If I need to run more than a single query that is fine, but I prefer not running a query for each category.
If you don't have performance problems you can use GROUP_CONCAT function like this,
SELECT id,GROUP_CONCAT(name)
FROM tools
GROUP BY id
you may read more abut GROUP_CONCAT from mysql official web site here
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.
Google unfortunately didn't seem to have the answers I wanted. I currently own a small search engine website for specific content using PHP GET.
I want to add a latest searches page, meaning to have each search recorded, saved, and then displayed on another page, with the "most searched" at the top, or even the "latest search" at the top.
In short: Store my latest searches in a MySQL database (or anything that'll work), and display them on a page afterwards.
I'm guessing this would best be accomplished with MySQL, and then I'd like to output it in to PHP.
Any help is greatly appreciated.
Recent searches could be abused easily. All I have to do is to go onto your site and search for "your site sucks" or worse and they've essentially defaced your site. I'd really think about adding that feature.
In terms of building the most popular searches and scaling it nicely I'd recommend:
Log queries somewhere. Could be a MySQL db table but a logfile would be more sensible as it's a log.
Run a script/job periodically to extract/group data from the log
Have that periodic script job populate some table with the most popular searches
I like this approach because:
A backend script does all of the hard work - there's no GROUP BY, etc made by user requests
You can introduce filtering or any other logic to the backend script and it doesn't effect user requests
You don't ever need to put big volumes of data into the database
Create a database, create a table (for example recent_searches) and fields such as query (the query searched) and timestamp (unix timestamp that the query was made) said, then for your script your MySQL query will be something like:
SELECT * FROM `recent_searches` ORDER BY `timestamp` DESC LIMIT 0, 5
This should return the 5 most recent searches, with the most recent one appearing first.
Create table (something named like latest_searches) with fields query, searched_count, results_count.
Then after each search (if results_count>0), check, if this search query exists in that table. And update or insert new line into table.
And on some page you can just use data from this table.
It's pretty simple.
Ok, your question is not yet clear. But I'm guessing that you mean you want to READ the latest results first.
To achieve this, follow these steps:
When storing the results use an extra field to hold DATETIME. So your insert query will look like this:
Insert into Table (SearchItem, When) Values ($strSearchItem, Now() )
When retrieving, make sure you include an order by like this:
Select * from Table Order by When Desc
I hope this is what you meant to do :)
You simply store the link and name of the link/search in MySQL and then add a timestamp to record what time sb searched for them. Then you pull them out of the DB ordered by the timestamp and display them on the website with PHP.
Create a table with three rows: search link timestamp.
Then write a PHP script to insert rows when needed (this is done when the user actually searches)
Your main page where you want stuff to be displayed simply gets the data back out and puts them into a link container $nameOfWebsite
It's probably best to use a for/while loop to do step 3
You could additionally add sth like a counter to know what searches are the most popular / this would be another field in MySQL and you just keep updating it (increasing it by one, but limited to the IP)
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.