简体   繁体   English

查询适用于本地主机,但不适用于生产服务器

[英]Query works on localhost, but not in production server

My English is not so good, but I'll try to explain the problem.我的英语不太好,但我会尽力解释这个问题。

I have three tables: One called Accounts , one called Accounts_Profiles and another called Accounts_Messages .我有三个表:一个称为Accounts ,一个称为Accounts_Profiles ,另一个称为Accounts_Messages My goal is to show the last users that the logged in user ($login_user_id) sent a message to.我的目标是显示登录用户 ($login_user_id) 向其发送消息的最后一个用户。
My tables basically consists of this:我的表基本上包括:

Accounts

id, name, email, nivel, verification id, name, email, nivel, 验证

Accounts_Profiles

id, user_id, status id, user_id, 状态

Accounts_Messagens

id, sender (int), receiver (int) id,发送者(int),接收者(int)

My query is currently like this: SELECT DISTINCT Accounts.id, Accounts.email, Accounts.name, Accounts.username, Accounts.avatar, Accounts.nivel, Accounts.verification FROM Accounts JOIN Accounts_Messages ON Accounts.id = Accounts_Messages.receiver ORDER BY Accounts_Messages.id DESC;我的查询目前是这样的: SELECT DISTINCT Accounts.id, Accounts.email, Accounts.name, Accounts.username, Accounts.avatar, Accounts.nivel, Accounts.verification FROM Accounts JOIN Accounts_Messages ON Accounts.id = Accounts_Messages.receiver ORDER BY Accounts_Messages.id DESC;

In CodeIgniter/PHP, it looks like this:在 CodeIgniter/PHP 中,它看起来像这样:

/*
    With the $this->db->distinct() I make sure that the users shown will not be repeated - It works!
*/
$this->db->distinct();
$this->db->select(
    array(
        "Accounts.id id",
        "Accounts.email email",
        "Accounts.name name",
        "Accounts.username username",
        "Accounts.avatar avatar",
        "Accounts.nivel nivel",
        "Accounts.verification verification",
        "Accounts_Profiles.status online",
        "(SELECT IF(COUNT(ACM.id) > 0, COUNT(ACM.id), null) FROM Accounts_Messages ACM WHERE ACM.receiver = '$login_user_id' AND ACM.sender = 'Accounts.id' AND ACM.is_read = '0') unread"
    )
);

/*
    Here I exclude the $login_user_id from the list, the admin users and also those who have not verified the account - It works!
*/
$this->db->join(
    "Accounts_Profiles",
    "Accounts_Profiles.user_id = Accounts.id", 
    "left"
);
$this->db->where(
    array(
        "Accounts.id !=" => $login_user_id,
        "Accounts.nivel !=" => 'administrator',
        "Accounts.verification !=" => 'false'
    )
);

/*
    Here, I sort messages by last sent. I check who the logged in user sent the last messages to and list those users - This only works on localhost.
*/
$this->db->join(
    "Accounts_Messages", 
    "Accounts.id = Accounts_Messages.receiver", 
    "left"
);
$this->db->order_by(
    "Accounts_Messages.id", 'DESC'
);

/*
    Returning the result
*/
return $this->db->limit($filters['limit'])->offset($filters['offset'])->get("Accounts")->result();

The problem: This works on localhost, but it doesn't work in production.问题:这适用于本地主机,但不适用于生产环境。 On localhost I use MySQL. On the production server I use MariaDB. In theory it shouldn't make any difference, since the clauses I used are compatible in both cases.在本地主机上,我使用 MySQL。在生产服务器上,我使用 MariaDB。理论上它应该没有任何区别,因为我使用的子句在这两种情况下都是兼容的。

Am I forgetting to do something or some configuration on the production server?我忘记在生产服务器上做某事或某些配置了吗?

Thank you!谢谢!

I tried to add this for conscience sake, but it didn't work:为了良心,我试图添加这个,但它没有用:
$this->db->group_by( "Accounts.id" );


Edit编辑

As you can see, in localhost the ordering of users is given by the last message sent to them.如您所见,在 localhost 中,用户的顺序由发送给他们的最后一条消息给出。 That's what I expect in production.这就是我对生产的期望。 在此处输入图像描述

On the production server the ordering is happening by conversation creation date (I don't know how).在生产服务器上,排序是按对话创建日期进行的(我不知道如何)。 It's wrong, as the first users I chat with always come last even if I send them a new message.这是错误的,因为即使我向他们发送新消息,我聊天的第一批用户也总是排在最后。 在此处输入图像描述

Solution解决方案

I managed to solve the problem by doing the following adjustment in the query:我设法通过在查询中进行以下调整来解决问题:

SELECT Accounts.id, Accounts.email, Accounts.name, Accounts.username, Accounts.avatar, Accounts.nivel, Accounts.verification, Accounts_Messages.id
FROM Accounts 
JOIN (
    SELECT m_to, MAX(dt_updated) as max_updated
    FROM Accounts_Messages
    GROUP BY m_to
) last_message
ON Accounts.id = last_message.m_to
JOIN Accounts_Messages 
ON Accounts.id = Accounts_Messages.m_to AND Accounts_Messages.dt_updated = last_message.max_updated
GROUP BY Accounts.id, Accounts_Messages.id
ORDER BY last_message.max_updated DESC;

It looks like this in CodeIgniter:在 CodeIgniter 中看起来是这样的:

<?php
    /*
        With the $this->db->distinct() I make sure that the users shown will not be repeated
    */
    $this->db->distinct();
    $this->db->select(
        array(
            "Accounts.id id",
            "Accounts.email email",
            "Accounts.name name",
            "Accounts.username username",
            "Accounts.avatar avatar",
            "Accounts.nivel nivel",
            "Accounts.verification verification",
            "Accounts_Messages.id acmid",
            "Accounts_Profiles.status online",
            "(SELECT IF(COUNT(ACM.id) > 0, COUNT(ACM.id), null) FROM Accounts_Messages ACM WHERE ACM.m_to = '$login_user_id' AND ACM.m_from = 'Accounts.id' AND ACM.is_read = '0') unread"
        )
    );

    /*
        Here I exclude the $login_user_id from the list, the admin users and also those who have not verified the account
    */
    $this->db->join(
        "Accounts_Profiles",
        "Accounts_Profiles.user_id = Accounts.id", 
        "left"
    );
    $this->db->where(
        array(
            "Accounts.id !=" => $login_user_id,
            "Accounts.nivel !=" => 'administrator',
            "Accounts.verification !=" => 'false'
        )
    );

    /*
        Here, I sort messages by last sent. I check who the logged in user sent the last messages to and list those users
    */
    $this->db->join(
        "(SELECT m_to, MAX(dt_updated) as max_updated FROM Accounts_Messages GROUP BY m_to) last_message", 
        "Accounts.id = last_message.m_to", 
        'inner'
    );
    $this->db->join(
        "Accounts_Messages", 
        "Accounts.id = Accounts_Messages.m_to AND Accounts_Messages.dt_updated = last_message.max_updated", 
        'inner'
    );
    $this->db->group_by(
        "Accounts.id, Accounts_Messages.id"
    );
    $this->db->order_by(
        "last_message.max_updated", 
        "DESC"
    );
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 路由可在localhost上运行,但不能在生产服务器上运行 - Routes works on localhost but not on production server 查询可在localhost上运行,但不能在服务器上运行 - Query works on localhost,but not on server 基本身份验证在本地主机中工作,但在生产服务器中失败 - Basic Authentication works in localhost but fails in Production Server 反序列化未在生产服务器上运行,可在本地主机上运行 - Unserialize not running on production server, works on localhost Slim PHP REST API无法在生产服务器中获取数据,可以在本地主机上工作 - Slim PHP REST API not fetching data in production server, works in localhost “ foreach”查询在Localhost上有效,但在服务器上无效-PHP,MySQLi - 'foreach' query works on Localhost but not on Server - PHP, MySQLi 服务器上的SQL查询返回SQL错误,而在本地主机上运行正常 - SQL query on server returns SQL Error while on localhost works fine Laravel 5.2查询在本地主机上正常工作,而不在远程服务器上 - Laravel 5.2 Query works fine on localhost not on remote server PHPMailer适用于localhost,但在服务器上但不在服务器上 - PHPMailer works on localhost but on but not on server 函数在localhost上起作用,但在服务器上不起作用 - Function works on localhost but not on server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM