How to select all articles and their similar articles from MySQL?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.