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 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Comma in php and query

    Hey,
    I want to have an ID like: 1,2,3,4,5 and update WHERE `id`='1,2,3,4 (Whatever the ids are above)
    Does anyone know how I can do this?

    Much appreciated.
    Thanks!

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Why would you want to?



    ------------------------------------
    Leonard Whistler

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    That way I don't have to have a query in a while loop.
    Because I'm getting the IDs in a while loop, imploding them with commas and now I want to update them.

    Thanks.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    as a result of that, you have to use a while loop, to get each result line …

  • #5
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Simple... Use the IN () context same as a whole bunch of OR statements. If your query is php just use this.

    PHP Code:
    $q "UPDATE table_1 SET some_field='some_value' WHERE id IN (1,2,3,4)"
    If in a procedure you have to do it slightly different.
    Code:
    DELIMITER $$
    
    CREATE PROCEDURE `update_fields` (IN in_ids TEXT)
      BEGIN
        SET @q = CONCAT ("DATE table_1 SET some_field='some_value' WHERE id IN (", in_ids, ")");
        PREPARE STMT FROM @q;
        EXECUTE STMT;
      END$$
    
    DELIMITER ;
    PHP Code:
    // Use a pdo prepare and execute.
    $q "CALL update_fields (:ids)";
    $p = array (':ids'=>'1,2,3,4'); 

  • #6
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You don't mention what RDBMS you're working with, can we assume MySQL? If so, you can use the MySQL IN clause, e.g.
    PHP Code:
    $list= array(1,2,3,4,5);
    $sql'UPDATE sometable SET somefield=1 WHERE id IN (' .join(",",$list). ')';
    // WHERE IN (1,2,3,4,5) 
    Is this what you're looking for?

  • #7
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Haha bdl... I got my answer in first! lol
    Funny cause I hit post and it said there was a new post already.

  • #8
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Yep that's it.
    Thanks guys!

  • #9
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Coyote6 View Post
    Haha bdl... I got my answer in first! lol
    Funny cause I hit post and it said there was a new post already.
    I thought it was bizarre how close our code was... `somefield`, `some_field`... weird.

  • #10
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    lol ROFL haha I didn't see that! WOW!


  •  

    Posting Permissions

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