View Full Version : Selecting data from mysql using data from an array

03-14-2010, 09:27 PM
Hey peoples.
I have an array of friends: EX. array([0] => "FRIEND_0", [1] => "FRIEND_1") etc
Can I select 6 friends in this array from mysql and print thier pictures to the screen?


03-15-2010, 07:38 AM
"select id,name from yourTable where name in (". implode(',',$arrFriends).")";?

03-15-2010, 09:36 AM
I now get the following error (I added or die(mysql_error()); on the end)

Unknown column 'hiddenusername' in 'where clause'

Where hiddenusername is one of my friends

03-15-2010, 09:42 AM
You'd need to add single quites around the string values in your array, before passing to the implode() function.

03-15-2010, 09:58 AM
Any ideas on how I'd go about doing that?

03-15-2010, 10:12 AM
Any ideas on how I'd go about doing that?
How are you making that array? Is it hard coded? Please post your code.

03-15-2010, 10:13 AM
By the way echoing the implode thing you gave me outputs similar to this.

03-15-2010, 10:36 AM
"select id,name from yourTable where name in (". $strFriends.")";
Or you may use array_walk() to add single quotes around all elements.

03-15-2010, 01:24 PM
My website is down in certain areas on certain ISPs, I'm waiting for DNS stuff to propogate.
I was at my nans this morning now I'm at my mums and it doesn't work here.
I will post when I go home again and see if it works there!! (:

03-15-2010, 02:23 PM
Tested it.. But it only retrieved this:
Array ( [0] => someuser [id] => 16 [user] => someuser )
I need to retrieve all his/her friends from db and then show thier profile pictures shown in the "profilepic" column.

03-15-2010, 03:46 PM
We presume that a loop has been involved for retrieval from the database?

03-15-2010, 08:28 PM
That is correct. It uses a while loop:

$arrFriends = unserialize($row['friends']);
$friends_query = mysql_query("select user from MyTable where user in (". $strFriends.")") or die(mysql_error());
while ($current_friends = mysql_fetch_array( $friends_query ))
I'm just using print_r() for now to see if it works...

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum