...

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



noisyscanner
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?

Thanks,
Noisyscanner

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

noisyscanner
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

abduraooft
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.

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

abduraooft
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.

noisyscanner
03-15-2010, 10:13 AM
By the way echoing the implode thing you gave me outputs similar to this.
somebody,somebodyelse,username21,dave101,etc...

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

noisyscanner
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!! (:

noisyscanner
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.

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

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


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum