简体   繁体   English

如何解决此MySQL错误?

[英]How do I fix this MySQL error?

I have this code for a news feed and it's combined with a code for a "load more" function. 我有此代码用于新闻订阅源,并且与“更多加载”功能的代码结合在一起。 The updates table is where the updates in the newsfeed exist. 更新表是新闻源中存在更新的位置。 The username_poster is the username of the person posting an update into which displays in the newsfeed. username_poster是将更新发布到新闻源中的人的用户名。 $last_msg_id represents the id of the last post in the newsfeed to represent what to load next. $last_msg_id代表新闻源中最后一个帖子的ID,代表接下来要加载的内容。

The problem I'm having is whenever my code calls to this script, it never loads it. 我遇到的问题是,只要我的代码调用此脚本,它就永远不会加载它。 The script works fine when I have the 当我有

username_poster IN
(SELECT user_id FROM scuela_following WHERE follower_id = '".$_SESSION['username']."')`

out of the code, but as soon as I add it in, it stops working. 超出代码,但是一旦添加,它就会停止工作。 Any help would be greatly appreciated. 任何帮助将不胜感激。

<?php
$last_msg_id=$_GET['last_msg_id'];
$sql=mysql_query("SELECT * FROM updates_table WHERE id < '$last_msg_id' AND username_poster IN
(SELECT user_id FROM scuela_following WHERE follower_id = '".$_SESSION['username']."')
ORDER BY id DESC LIMIT 5");
$last_msg_id="";
while($row=mysql_fetch_array($sql))
{


} 
?>

I see that you miss the session_start(); 我看到您错过了session_start(); at the very top of your script. 在脚本的最顶部。

In this SQL it looks like you're looking for a "username_poster" in your subquery, when you're only selecting "user_id." 在此SQL中,当您仅选择“ user_id”时,您似乎在子查询中寻找“ username_poster”。

    AND username_poster IN
    (SELECT user_id
    FROM scuela_following

Alter the SQL to look for "user_id" instead of "username_poster", or return "username_poster" instead of "user_id" in the subquery. 更改SQL以在子查询中查找“ user_id”而不是“ username_poster”,或者返回“ username_poster”而不是“ user_id”。

And as mentioned above, you should really use queries with parameters to prevent SQL injection. 并且如上所述,您应该真正使用带有参数的查询来防止SQL注入。

I suspect that there is a problem here: 我怀疑这里有问题:

"... WHERE follower_id = '".$_SESSION['username']."' ..."

Firstly you are not properly escaping and may have an SQL injection vulnerability. 首先,您没有正确地转义,并且可能存在SQL注入漏洞。 You should use mysql_real_escape_string or parameterized queries. 您应该使用mysql_real_escape_string或参数化查询。

Secondly, it looks like you are comparing a username (eg "foobar") to an ID (eg 10042). 其次,看起来您正在将用户名(例如“ foobar”)与ID(例如10042)进行比较。 You probably need to join with another table that relates usernames to user IDs. 您可能需要加入另一个将用户名与用户ID相关联的表。

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

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