Ryan
Ryan

Reputation: 499

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)

Upvotes: 1

Views: 6264

Answers (4)

Tor Valamo
Tor Valamo

Reputation: 33749

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

Upvotes: 1

Daniel Dinu
Daniel Dinu

Reputation: 1791

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.

Upvotes: 0

Eimantas
Eimantas

Reputation: 49354

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

Upvotes: 1

Related Questions