In general, you are on the right path, the selection criteria you really should compare blog_comments.post_id and blog_posts.postId , but in the condition you for some reason compare other fields.
Howbeit. On some page you show the post and its comments. To show the desired post, you probably have in the URL parameters its Id , which in php-code you get, for example, in the form $_GET['post_id'] .
Next you need to select all the information about the post. To do this, you must write a query like:
SELECT * FROM blog_posts WHERE postId = :id
Make it a rule of good tone to never immediately substitute the passed parameters directly to the request, at least without prior verification. If you write a code of the form $sql = "select * from blog_posts where postId = ${_GET['id]}" , then when sending as an id a line like 1; delete from posts; 1; delete from posts; will delete your data. If you want to substitute a value directly into a query, then at least check it with is_numeric() or intval() . Do not forget to read about SQL injection
To work with the database, use modern extensions, for example, PDO . While reading old books, you can often come across the use of mysql_* functions (not to be confused with mysqli_ ). They are already outdated.
Given the above, your sample code should look something like this:
$sth = $dbh->prepare('SELECT * FROM blog_posts WHERE postID = :id'); $sth->bindParam(':id', $id, PDO::PARAM_INT); $sth->execute(); $result = $sth->fetch(PDO::FETCH_ASSOC);
Now you want to load related comments. Use a second query for this. In general, there is no need to link tables here:
$sql = "SELECT * FROM blog_comments WHERE post_id = :id ORDER BY date DESC"; $sth = $dbh->prepare($sql); $sth->bindParam(':id', $id, PDO::PARAM_INT); $sth->execute(); $result['comments'] = $sth->fetchAll();
Now the $result variable will contain post data and comments. It remains to show them to the user. Many people interfere with the work logic (php-code) and data mapping (presentation, html) in one file. This is a bad approach, so it is better to immediately look in the direction of template engines like smarty , twig , etc.
So now it remains to issue the html-code and output the data.
And note on the naming of the fields of your tables. The primary key is usually referred to as id . It makes no sense to call them postId or commentId , so often called foreign key fields ( postId or post_id ). That is, the primary key is simply id , and the foreign key usually contains the name of the external table and the suffix id , _id , _ID , etc.
Well, lastly, if you have read these lines before, then begin to learn php, take some time to study the databases. Read some small book that will give you an understanding of the basics of their use. And it will be easier for you to continue studying.