douzong7283 2013-07-04 03:29
浏览 50
已采纳

使用JOIN从ID显示用户名

I am trying to use a JOIN to connect the "user_id" from my "users" table, with my "topic_by" in my "topics" table, I searched around and set up the following code.

$sql = "SELECT topics.topic_id, topics.topic_subject, topics.topic_date, topics.topic_cat, topics.topic_by
            FROM topics 
            INNER JOIN users ON topics.topic_by=users.user_id
            WHERE topics.topic_cat = " . mysql_real_escape_string($_GET['id']) . " ORDER BY topics.topic_date DESC;";

It's not returning any errors, but when I try to call the user_name..

. $row['users.user_name'] .

There is just a blank space where the data should be, what am I doing wrong? Thanks.

  • 写回答

1条回答 默认 最新

  • duanlou2917 2013-07-04 03:30
    关注

    You didn't select the user_name column in your SELECT statement.

    Try this:

    $sql = "SELECT topics.topic_id, topics.topic_subject, topics.topic_date, topics.topic_cat, topics.topic_by, users.user_name
      FROM topics 
      INNER JOIN users ON topics.topic_by=users.user_id
      WHERE topics.topic_cat = " . mysql_real_escape_string($_GET['id']) . " ORDER BY topics.topic_date DESC;";
    

    To select this column data in your $row array, you would write it like: $row['user_name']. The keys in your $row array are the column names (or aliases, if provided) but do not include the table name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码