How to SELECT an articles and its comments from mysql?
-
I read an article and its comments from mysql database with two separate queries as $result = mysql_query("SELECT * FROM articles WHERE article_id='$id'"); $row = mysql_fetch_array($result); $title=$row['title']; ........ AND $result = mysql_query("SELECT * FROM comments WHERE article_id='$id'"); while($row = mysql_fetch_array($result)) { $comment_title=$row['title']; ......... } Is the best way to read this set of data from database? OR Is it possible to catch the data through one query or one transaction? NOTE: My issue is that first query for article is only for one row; but the second one needs a loop to process (and display in html) several comments.
-
Answer:
$result = mysql_query("SELECT articles.title as article_title, comments.title as comment_title FROM articles LEFT JOIN comments ON articles.article_id = comments.article_id WHERE articles.article_id = '$id'"); $row = mysql_fetch_array($result); $article_title=$row['article_title']; $comment_title=$row['comment_title '];
All at Stack Overflow Visit the source
Other answers
You can do it in this way - SELECT a.*, c.* FROM articles a LEFT JOIN comments c ON a.article_id = c.article_id WHERE a.article_id='$id';
Devart
Is the best way to read this set of data from database? Sure, it is. Is it possible to catch the data through one query Yes, it's possible but there is no point in doing that. Why do you ask?
Your Common Sense
Related Q & A:
- How To Select Odd Numbers In Excel?Best solution by Yahoo! Answers
- How to select specific filenames nested within several folders?Best solution by Geographic Information Systems
- How to select specific data from listbox?Best solution by stackoverflow.com
- how to select max and min?Best solution by Stack Overflow
- Can anyone tell me how to select a University in Aus or NZ to study MA or MS or MBA in Communication?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.