dsdsds12222 2011-08-16 14:15
浏览 20
已采纳

如何从多个表中选择数据并使用PHP显示它?

I'm trying to make a Private Message Application. And I need the Inbox to show both "main messages" and "answers on messages".

My tables are as listed:

post

id - header - content - from_user - to_user - receiver_opened - user_bywho - timestamp.

post_answer

id - answer_from_user - answer_to_user - answer_user_bywho - answer_header - answer_content - answer_id - timestamp_svar.

And now I'm having troubles. Because I don't know how to make this possible.

My mysql_query looks like this right now:

$user = $_SESSION['username'];    

$sql = mysql_query("SELECT * FROM post,post_answer WHERE ".
                  "answer_to_user='$user' AND to_user='$user'")
       or die(mysql_error()); 

Thanks in advance.

  • 写回答

5条回答 默认 最新

  • doutan3463 2011-08-16 14:25
    关注

    The query is not valid as it will be ambiguous about the id field.

    USE JOIN

    To do this, you can either use JOIN

    SELECT * 
        FROM post 
        INNER JOIN post_answer
        WHERE post.to_user = post_answer.answer_to_user
    

    THE STUPID WAY (easy to understand)

    SELECT post.id, 
           header, 
           content, 
           from_user, 
           to_user, 
           receiver_opened, 
           user_bywho, timestamp,
           post_content.id, 
           answer_from_user, 
           answer_to_user, 
           answer_user_bywho, 
           answer_header, 
           answer_content, 
           answer_id, 
           timestamp_svar
        FROM post, 
             post_content
        WHERE answer_to_user=$user AND 
              to_user=$user
    

    Hope this helps

    UPDATE

    Lets suppose your table contains fields like-

    1. firstname
    2. lastname
    3. number

    See if you get the result in an array say $results.

    foreach ($results as $result)
    {
        echo $result->firstname;
        echo $result->lastname;
        echo $result->number;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 俄罗斯方块中无法同时消除多个满行
  • ¥15 c#转安卓 java html
  • ¥15 os.listdir文件路径找不到
  • ¥15 使用gojs3.0,如何在nodeDataArray设置好text的位置,再go.TextBlock alignment中进行相应的改变
  • ¥15 psfusion图像融合指标很低
  • ¥15 银河麒麟linux系统如何修改/etc/hosts权限为777
  • ¥50 医院HIS系统代码、逻辑学习
  • ¥30 docker离线安装mysql报错,如何解决?
  • ¥15 构建工单的总账影响在哪里查询或修改
  • ¥15 三个简单项目写完之后有重赏之后联系我