using php array in mysql command ??

Discussion in 'PHP' started by dracula51, Oct 23, 2009.

  1. #1
    here's an example code

    $a= array(2, 7, 15, 17);
    
    mysql_query("select * from table_name where id=$a");
    
    PHP:
    i tried to make that command to get values from those tables only whos id r on that array

    alas! its not working :(

    can anyone help me, is there anyway to do such action
     
    dracula51, Oct 23, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    following will do help

    
    $a= array(2, 7, 15, 17);
    $id_list = implode(',', $a)
    mysql_query("select * from table_name where id in ($id_list)");
    
    PHP:
    if you can explain the purpose, we may be able to pick better approach..
     
    mastermunj, Oct 23, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    From the looks of it, that's exactly what the OP wanted :)
     
    JAY6390, Oct 23, 2009 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    or you can do this:
    
    $a= array(2, 7, 15, 17);
    $num_elements = count($a); //number of elements inside the array
    
    for($i=0; $i<$num_elements; $i++) //loop to get all elements
    {
             mysql_query("select * from table_name where id=$a[$i]"); //query the elements
    }
    
    PHP:
     
    xenon2010, Oct 23, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The only problem with that is that each element is a query on your database (which will be a massive waste of resources if you have say 100 items in the array)
     
    JAY6390, Oct 23, 2009 IP
  6. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thank you both mastermunj & xenon2010

    well, mastermunj...here is my purpose...maybe there's an easy & shortcut way
    first i want it to collect IDs from a mySQL table, then it will do another mySQL query where id=those collected IDs

    maybe like this:
    mysql_query("select id from table_name where user=$userid");

    it will collect some IDs where user is my declared $user

    then it will do another query where it'll select * (all) where id=those collected id

    & as JAY6390 said...i need something that wont waste massive resources..

    thats what i wanted....anymore info need, plz ask me ??

    & im really greatful...tnx again :)
     
    dracula51, Oct 23, 2009 IP
  7. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just explode the list and query for each element by using foreach loop.
     
    Om ji Kesharwani, Oct 23, 2009 IP
  8. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #8
    that means you want to select data from 2 tables based on userid. Why not use join in that case? it can be done in single query then.

    Please share both table structure with us along with proper example, so that we can help building query.
     
    mastermunj, Oct 23, 2009 IP
  9. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    can u giv an example plz

    1st one: mysql_query("SELECT id FROM favorites WHERE userid=$userid");
    2nd one: mysql_query("SELECT * FROM data WHERE id=XXXX");

    i used XXXX in 2nd query as i dont know what will be there.


    table structure:
    table name: favorites
    field: id, userid
    (total 2 fields in this table)


    table name: data
    field: id, story, author, addedtime, comment
    (total 5 fields in this table)


    thats all.
     
    dracula51, Oct 23, 2009 IP
  10. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    SELECT * from favorites left join data on favorites.id = data.id where userid = $userid
    Code (markup):
     
    heavydev, Oct 23, 2009 IP
  11. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    never mind i found it...
    tnx u heavydev too
    its working nice now with a single query just what i wanted :)
     
    dracula51, Oct 23, 2009 IP