【问题标题】:JOIN query returning 100450(inetuser)JOIN 查询返回 100450(inetuser)
【发布时间】:2013-01-24 00:31:54
【问题描述】:

我正在制作一个私人消息系统。我的表单正确地发布到我的表中,但返回查询拉出了一些奇怪的东西。我对加入表格有点陌生...

带有 sql 查询的 PHP 代码:

function fetch_messages()  {
    $sql = "SELECT
                `conversations`.`con_id`,
                `conversations`.`con_subject`,
                MAX(`conversations_messages`.`message_date`) AS `con_last_reply`
            FROM `conversations`
            LEFT JOIN `conversations_messages` ON `conversations`.`con_id` = `conversations_messages`.`con_id`
            INNER JOIN `conversations_members` ON `conversations`.`con_id` = `conversations_members`.`con_id`
            WHERE `conversations_members`.`user_id` = 31
            AND `conversations_members`.`con_deleted` = 0
            GROUP BY `conversations`.`con_id`
            ORDER BY `con_last_reply` DESC";

    $result = mysql_query($sql);

    $conversations = array();

    while (($row = mysql_fetch_assoc($result))  !== false)  {

        $conversations[] = array(
        `id`        => $row['con_id'],
        `subject` => $row['con_subject'],
        `con_last_reply` => $row['last_reply']
        );
    }
    return $conversations;
}

返回数组:

Array ( [0] => Array ( 
                [uid=8927072(user007) gid=100450(inetuser) groups=100450(inetuser) ] => 1
                [] => ) )

(它几乎就像是在返回我的 ftp 信息?user007 是我打开的 ftp 连接名称,但这不是表格中的内容!!)

【问题讨论】:

  • 您的数组输出与您的代码完全不匹配。您确定要转储正确的数组吗?此外 mysql_ 函数已被弃用并替换为 mysqli_ (或者您可以使用 PDO)
  • 我同意,您的代码不可能创建数组。它没有键 idsubjectcon_last_reply。相反,它有一些看起来像 Unix id 命令的输出作为键。
  • @datasage 是的,我正在倾销“print_r(fetch_messages());”是的,我知道这就是为什么它很奇怪......是的,我知道 mysql_functions 已经失效我还没有切换。
  • @barmar 我知道键错了...不知道我是否使用关键字作为变量名?

标签: php group-by max jointable


【解决方案1】:

您使用了错误的引号类型。应该是:

    $conversations[] = array(
    'id'        => $row['con_id'],
    'subject' => $row['con_subject'],
    'con_last_reply' => $row['last_reply']
    );

反引号就像在 shell 或 Perl 中一样,它们运行命令并将其输出作为字符串返回。

直到这个问题我才知道 PHP 有这个功能。

Documentation

【讨论】:

  • 我们有一个获胜者非常感谢:) ` 和 ' 看起来很混乱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 2015-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多