简体   繁体   English

PHP查询不起作用

[英]PHP Query Doesn't Work

I have the following code, which should get some results from 2 tables, I'm currently trying to load their lastactive (time), user id, user avatar. 我有以下代码,它应该从2个表中获得一些结果,我目前正在尝试加载他们的最后一个(时间),用户ID,用户头像。

I'm using the following code to display this on a page: 我正在使用以下代码在页面上显示:

<?php
            $t = 1800;
            $sql = 'SELECT mh_users.id, mh_users.lastactive, mh_users.avatar, mh_users.gang FROM `mh_users` ';
            $sql .= 'LEFT JOIN `mh_users_stats` ON mh_users.id = mh_users_stats.id WHERE mh_users.lastactive > `'.$t.'` ORDER BY `mh_users.lastactive` DESC';
            $res = $db->query($sql) or exit($db->error);
            while($row = $res->fetch_object()) {
                $online_user = new User($row->id);
                echo '<tr>'
                     . '<td><img src="'.$online_user->avatar.'" width="40" height="40" alt="[avatar]" /></td>'
                     . '<td>'.$online_user->formattedname.'</td>'
                     . '<td>'.$gang_name.'</td>'
                     . '</tr>'
                ;
            }
            ?>

It seems that with this query, the page is blank, $db->error doesn't return anything, the page is just white. 看来用这个查询,页面是空白的,$ db-> error不返回任何内容,页面只是白色。

However, if I do it in this query: 但是,如果我在此查询中执行此操作:

$sql = 'SELECT mh_users.id, mh_users.lastactive, mh_users.avatar, mh_users.gang FROM `mh_users` ';
$sql .= 'LEFT JOIN `mh_users_stats` ON mh_users.id = mh_users_stats.id';

It does work, however, it doesn't ORDER at last active time, how can I make this query to list the results based on lastactive time (So the user that accessed a page the latest, will be shown at top) 它确实有效,但是,它在最后一个活动时间没有ORDER,如何根据上一个活动时间进行此查询以列出结果(因此访问最新页面的用户将显示在顶部)

Sorry for the noob question, but I can't get it fixed at the moment :( 抱歉没有问题,但我现在无法解决这个问题:(

Many thanks! 非常感谢!

Instead of 代替

ORDER BY `mh_users.lastactive`

you should have backtick alias and field name separately like this 你应该像这样单独使用反引号别名和字段名称

ORDER BY `mh_users`.`lastactive`

or don't use backticks at all because you already don't use them everywhere in your query. 或者根本不使用反引号,因为您已经不在查询中的任何地方使用反引号。

And as we find out in comments you forgot to add WHERE condition in your update statement. 正如我们在评论中发现的那样,您忘记在更新语句中添加WHERE条件。 Something like this $sql = 'UPDATE mh_users SET lastactive = '.time().' WHERE id='.$_SESSION['uid']; 像这样的东西$sql = 'UPDATE mh_users SET lastactive = '.time().' WHERE id='.$_SESSION['uid']; $sql = 'UPDATE mh_users SET lastactive = '.time().' WHERE id='.$_SESSION['uid']; should work. 应该管用。

ORDER BY `mh_users`.`lastactive`

ORDER BY `mh_users.lastactive`

This should work for you (I just removed the back ticks around the variable): 这应该适合你(我只是删除了变量周围的后面的滴答声):

$sql = 'SELECT mh_users.id, mh_users.lastactive, mh_users.avatar, mh_users.gang FROM `mh_users` ';
$sql .= 'LEFT JOIN `mh_users_stats` ON mh_users.id = mh_users_stats.id WHERE mh_users.lastactive > '.$t.' ORDER BY `mh_users.lastactive` DESC';

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM