tags:

views:

665

answers:

3

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)

+1  A: 

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

Eimantas
A: 

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.

Dinu Florin
+1  A: 

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
Tor Valamo
Thanks, the errors disappear but the actual script doesn't seem to do anything =(I used:$friendnotes = mysql_query("SELECT nid,user,subject,message FROM friendnote WHERE user IN ('SELECT u1,u2 FROM friends WHERE u1 = $userinfo[username] OR u2 = $userinfo[username]')ORDER BY timestamp ASC LIMIT 0,5");When I try to use a MySQL function such as mysql_fetch_array() it doesn't return an error, but doesn't return any results either (and yeah I know theres entries in the database that matches the WHERE)
Ryan
See the updated query.
Tor Valamo