如何加入(内爆)MySQL 数组?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
GROUP Concat 不行吗?
http://dev.mysql.com /doc/refman/5.0/en/group-by-functions.html#function_group-concat
Wouldn't GROUP Concat work?
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
您始终可以使用子选择并将其传递给
IN()
运算符。You can always use sub-select and pass it to
IN()
operator.我认为你加入了错误的组织。您将加入整个朋友 ROW,因此结果为“1、Johnny、23years 等”。我假设您想要朋友 ID '1,2,3,4,5' 的列表。正如 Eimantas 所说,最好使用子查询。
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.
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.