获取主题ID的类别

So i want to get the category name to be showed on the id of the topic. Currently i have this

        $cato = $db->prepare('SELECT
                        topics.topic_cat,
                        topics.topic_id,
                        categories.cat_id,
                        categories.cat_name
                    FROM
                        topics
                    LEFT JOIN
                        categories
                    ON
                        topics.topic_cat = categories.cat_id
                    WHERE
                        topics.topic_id =:topid');

$cato->bindParam(':topid',  $row2["topic_id"], PDO::PARAM_INT); 
$cato->execute();
$result2 = $cato->fetch();

Categories.cat_id is the id of the category.

Categories.cat_name is the name i want get.

Topics.topic_cat is the category id where the topic is posted ( the same id from categories.cat_id)

Topics.topic_id is the id of the topic (topic.php?id=28)

My result needs to be

topic 28 is in category hello (hello = categories.cat_name).

So, to be short: the topic id is 28 it searches in topics the row with topic_id 28 and grabs the topic_cat that is in the same row as topic_id 28. Then it looks in categories and grabs the categories_name is topic_cat. then it posts the categories_name

I hope i am clear enough.

Can you try:

SELECT topics.topic_cat,
       topics.topic_id,
       categories.cat_id,
       categories.cat_name
FROM topics, categories
WHERE topics.topic_cat = categories.cat_id
      AND topics.topic_id =:topid

This should work unless you have topic without category.