How to select two tables from database?

Hi guys im new in php and I’m trying to select two tables from the database but without success if there is someone who can help me with that…table name is f_posts and posts…my code is…

                       $query = "SELECT * FROM f_posts WHERE post_category_id = 1 ORDER BY f_post_id ";
                        $select_all_posts_query = mysqli_query($connection, $query);

                        while($row = mysqli_fetch_assoc($select_all_posts_query)) {
                        $post_id = $row['f_post_id'];
                        $post_title = $row['post_title'];
                        $post_author = $row['post_user'];
                        $post_date = $row['post_date'];
                        $post_image = $row['post_image'];
                        $post_content = substr($row['post_content'],0,340);


It’s not clear from the code provided exactly what you are trying to do with the two tables, as that code only appears to deal with one.
But the way you connect two or more tables in SQL is with JOINs.

1 Like

Ok let me explain,have two tables first table is f_posts <— (featured posts) second table is posts,now i want to show both on the blog list when click for example link news or all news whatever…

How do the two tables relate to each other? Or do they not, and you just want to put the featured posts first, then the non-featured posts?

If the latter, I’d have thought a better way would be to put all the posts in the same table, and just have a Boolean column that shows whether they are featured or not. Then you can extract them all in a single query, order by whether they are featured or not, and display them.


Hi guys first of all thx for your help,
As I said above I’m still new in php and I try to learn every day more as much as I can.
I want something like this

if post not featured i want to display him on second and third if featured hes on first.I did in cms two options posts and featured posts and it works as I imagined but I see that I’ve done everything wrong…
Can you help me how to do that in your way becouse on this way i dont know how to display both featured and not featured in one blog list for example all news or all category news…
That’s why I was trying to connect these two tables to show all news toghether

A JOIN is generally for joining data together from relational tables. But in this instance the two tables don’t really relate data, but both server a very similar purpose, so I would probably go with droopsnoot’s sugestion:-

This is having a suitable database design and structure, which is something we don’t always get right first time. Sometimes the needs of a project will change over time, or as you gain experience you think of better ways to organise data. It’s all part of the learning curve.

Though I don’t know your database architecture, I have the feeling it isn’t optimal.

That said, IF the fields you want from both the featured posts and posts tables are for the most part identical, maybe a UNION would work. For example, if you wanted one featured post and then ten “regular” posts, a query something like this could work

(SELECT somefield FROM featured_posts_table ORDER BY somefield LIMIT 1)
(SELECT somefield FROM posts_table ORDER BY somefield LIMIT 10);
1 Like

Hi Mittineague thank you for help my database structure for posts looks this…

featured post is same just first column is f_post_id

Finaly i found solution for my problem tnx droopsnoot Mittineague SamA74 for trying to help me…
It was easy case:featured and published then select posts by post_status.


The proper solution would be to have ONE table with a field to distinguish a featured post.

@Mittineague it’s bad to teach a child bad things.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.