dongyuan7981 2015-07-24 17:55
浏览 46

连接两个表以匹配整数以获取用户名

I am having trouble figuring out how I can join two tables and SELECT different rows from different database table.

I am wanting to add my users table to match the id with the post_creator and then to get the username from the users table for which ever id and post_creator numbers match.

So if my users table had an id of 10 and that was the username for Jim. If the post_creator value was 10, I am wanting Jim to be found and echoed out.

Right now this is killing the code and I do not know what I am doing wrong.

while ($row = $stmt->fetch()) {
        //added in topic title variable
        $topic_title;
//  foreach($stmt as $row) {
        //Prepared SELECT stmt to get forum posts
        $stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
        FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
        if($stmt2===false) {
            die();
        } else {
        //var_dump($stmt2);

            $stmt2->bind_param("ii", $cid, $tid);
            $stmt2->execute();
            $stmt2->store_result();
            $stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
            if (!$stmt2) {
            throw new Exception($con->error);
            }
        }
    $num_rows2 = $stmt2->num_rows;
    if($num_rows2) {
        $count2=0;
        while($stmt2->fetch()) {
        $count2++;
        $post_id;
        $post_category_id;
        $post_topic_id;
        $post_creator;
        $post_content;
         $post_date = fixDate($post_date);
        //$post_date;
            echo "<tr><td valign='top' style='border: 1px solid #000000;'>
            <div style='min-height: 125px;'>".$topic_title. "Post ".$count2."<br />
            by ".$post_creator." - " .$post_date. "<hr />" . $post_content ."</div></td>
            <td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
            <tr><td colspan='2'><hr /></td></tr>";
        }
  • 写回答

3条回答 默认 最新

  • dsfb20227 2015-07-24 18:02
    关注

    You can join the tables... Such as this (joins users, categories, and topics)

    SELECT forum_posts.id post_id, forum_posts.category_id post_category_id, forum_posts.topic_id post_topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date, forum_post_categories.category_name, forum_post_topics.topic_name, users.id user_id, users.username user_username FROM forum_posts LEFT JOIN users ON users.id = forum_posts.post_creator RIGHT JOIN forum_post_categories ON forum_post_categories.id = forum_posts.category_id RIGHT JOIN forum_post_topics ON forum_post_topics.id = forum_posts.topic_id WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?

    Note: this is untested.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算