如何加入(内爆)MySQL 数组?

发布于 2024-08-18 06:06:47 字数 544 浏览 4 评论 0原文

implode() 函数适用于普通数组,但不适用于使用 mysql_fetch_array 创建的数组(我也尝试过 mysql_fetch_row)

如何让它们工作?

上面定义:

$friends = mysql_query("SELECT * FROM friend 

WHERE u1='$userinfo[username]' OR u2='$userinfo[username]' ");

再往下:(

$friendsrow = mysql_fetch_array($friends);
$friendsrow = join(",",$friendsrow);

$friendnotes = mysql_query("SELECT nid,user,subject,message FROM friendnote WHERE user IN ($friendsrow) ORDER BY timestamp ASC LIMIT 0,5");

如果您想知道,那是针对社区网站的)

The implode() function works on normal arrays, but it doesn't work on arrays created with mysql_fetch_array (I also tried mysql_fetch_row)

How do you get them to work?

Defined Above:

$friends = mysql_query("SELECT * FROM friend 

WHERE u1='$userinfo[username]'
OR u2='$userinfo[username]' ");

And further down:

$friendsrow = mysql_fetch_array($friends);
$friendsrow = join(",",$friendsrow);

$friendnotes = mysql_query("SELECT nid,user,subject,message FROM friendnote WHERE user IN ($friendsrow) ORDER BY timestamp ASC LIMIT 0,5");

(In case your wondering, that is for a community site)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染火枫林 2024-08-25 06:06:47

您始终可以使用子选择并将其传递给 IN() 运算符。

You can always use sub-select and pass it to IN() operator.

帅气称霸 2024-08-25 06:06:47

我认为你加入了错误的组织。您将加入整个朋友 ROW,因此结果为“1、Johnny、23years 等”。我假设您想要朋友 ID '1,2,3,4,5' 的列表。正如 Eimantas 所说,最好使用子查询。

SELECT nid,user,subject,message 
FROM friendnote 
WHERE user IN ((SELECT u2 FROM friends WHERE u1 = $userinfo[username])
               UNION
               (SELECT u1 FROM friends WHERE u2 = $userinfo[username]))
ORDER BY timestamp ASC LIMIT 0,5

I think you're joining the wrong thing. You're joining the entire friend ROW, so the result is '1,Johnny,23years,etc'. I'm assuming you want a list of the friend ID's '1,2,3,4,5'. As Eimantas said it's better to use a subquery.

SELECT nid,user,subject,message 
FROM friendnote 
WHERE user IN ((SELECT u2 FROM friends WHERE u1 = $userinfo[username])
               UNION
               (SELECT u1 FROM friends WHERE u2 = $userinfo[username]))
ORDER BY timestamp ASC LIMIT 0,5
-黛色若梦 2024-08-25 06:06:47

mysql_fetch_array 返回普通数组,没有什么特别的。也许您的查询没有返回任何内容,在这种情况下 mysql_fetch_array 返回 false。在尝试破坏结果之前检查一下。

mysql_fetch_array returns normal arrays, there is nothing special about them. Maybe your query isn't returning anything, and in that case mysql_fetch_array returns false. Check for that before trying to implode the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文