Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Selecting data from mysql using data from an array

    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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    PHP Code:
    "select id,name from yourTable where name in ("implode(',',$arrFriends).")"
    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    You'd need to add single quites around the string values in your array, before passing to the implode() function.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Any ideas on how I'd go about doing that?

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Quote Originally Posted by noisyscanner View Post
    Any ideas on how I'd go about doing that?
    How are you making that array? Is it hard coded? Please post your code.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    By the way echoing the implode thing you gave me outputs similar to this.
    somebody,somebodyelse,username21,dave101,etc...

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    PHP Code:
    $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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #9
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!! (:

  • #10
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    We presume that a loop has been involved for retrieval from the database?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #12
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That is correct. It uses a while loop:

    PHP Code:
    $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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •