简体   繁体   English

您如何加入(爆破)MySQL阵列?

[英]How do you join (implode) a MySQL Array?

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) implode()函数适用于普通数组,但不适用于使用mysql_fetch_array创建的数组(我也尝试过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]' "); 在哪里u1 ='$ userinfo [username]'或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) (如果您想知道,那是针对社区站点的)

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

I think you're joining the wrong thing. 我认为您加入了错误的事情。 You're joining the entire friend ROW, so the result is '1,Johnny,23years,etc'. 您正在加入整个朋友ROW,因此结果为“ 1,Johnny,23years,etc”。 I'm assuming you want a list of the friend ID's '1,2,3,4,5'. 我假设您要列出朋友ID的“ 1,2,3,4,5”。 As Eimantas said it's better to use a subquery. 正如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

mysql_fetch_array returns normal arrays, there is nothing special about them. mysql_fetch_array返回普通数组,它们没有什么特别的。 Maybe your query isn't returning anything, and in that case mysql_fetch_array returns false . 也许您的查询未返回任何内容,在这种情况下mysql_fetch_array返回false Check for that before trying to implode the result. 在尝试使结果内爆之前先进行检查。

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

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