duanjianao0592 2012-05-06 17:17
浏览 22
已采纳

Mysql / PHP从post.postid选择行的列中获取post.userid

I am using Dreamweaver and MAMP to create a site. I have a table called posts inside I have two columns, postid, and userid. I'm putting all the posts on a page and I want to show which user posted which post, and write that on each post respectively. For example the first post that should show up has a postid of 2 and a userid of 1. Currently it is displaying the userid of the user who is logged in on all the posts, I am using:

mysql_select_db($database_akaearthdb, $akaearthdb);
$query_PostsUserid = "SELECT userid FROM posts WHERE posts.userid ORDER BY posts.postid";
$row_PostsUserid = mysql_fetch_assoc($PostsUserid);

and

echo $row_PostsUserid['userid']

To display it. I want $row_PostsUserid['userid'] to be the userid of the person who posted it.

Let me know if you need any more information, I'm new to the language so sorry if I'm doing everything wrong, TIA.

  • 写回答

2条回答 默认 最新

  • dsfsdfsd34324 2012-05-06 17:47
    关注

    Before executing a query from any language,

    Test the query on your local database command line.

    Your query,

    "SELECT userid FROM posts WHERE posts.userid ORDER BY posts.postid"
    

    will result in a syntax error.

    EDIT:

    Do the following,

    CREATE TABLE users (
             id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
             user_name VARCHAR(255),
             age INT,
             any_detail VARCHAR(255)
           );
    
    CREATE TABLE posts (
             id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
             post TEXT,
             created_at TIMESTAMP(8),
             user_id INT,
             FOREIGN KEY (user_id) REFERENCES users(id)
           );
    

    Now,

    To display the posts,

    Array =  SELECT * FROM posts ORDER BY created_at DESC;
    

    Store the result from the above query into your languages array/variable. Im a rails developer, Im not sure about PHP's syntax.(Google it and find out!)

    After storing the results into an array,

    Display the following for each element in the array as follows,

    Post content:
    << Array.post >>
    Written about:
    << Array.created_at >> ago
    Written by:
    << SELECT user_name FROM users WHERE user_id = Array.user_id >>

    That should help or take you somewhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上