简体   繁体   中英

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)

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)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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