mysql update command

Discussion in 'PHP' started by dracula51, Dec 10, 2009.

  1. #1
    in the field "data" value is "12,17,18,"
    i want to add 21 there...so it will be "12,17,18,21,"

    i tried:
    $abc = "21,";
    UPDATE myTable SET data = data.$abc WHERE id=1
    PHP:
    not working :(

    help ...!
     
    dracula51, Dec 10, 2009 IP
  2. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Try this

    UPDATE myTable SET data = data.'$abc' WHERE id=1
     
    kingsoflegend, Dec 10, 2009 IP
  3. lunarleviathan

    lunarleviathan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    UPDATE myTable SET data = CONCAT_WS(',',data,'$abc') WHERE id = 1;

    should give the result: 12,17,18,21

    CONCAT_WS function defination is CONCAT_WS(seperator, string1, string2, ...)
     
    lunarleviathan, Dec 10, 2009 IP
  4. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank you very much :)

    but still a problem.
    by this method...if there's nothing in mysql field...it start with a comma (,)
    so later it will be looks like: ,12,14,16,55
    is there any way ??
     
    dracula51, Dec 10, 2009 IP
  5. vengs

    vengs Greenhorn

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    The whole algorithm is not that bright
    but if u want to carry on
    let the , remain in mysql field...
    and use substr after retrive....or may be substr before mysql insert...
     
    vengs, Dec 11, 2009 IP
  6. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    but cant understand how to do it with substr()
    becoz the length is unknown
    it cud be ,12,15,97,44 or ,15,24 or ,14,15,16,24,54,86,35,.......
    so the question is how to remove the beginning comma only
     
    dracula51, Dec 11, 2009 IP
  7. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #7
    n3r0x, Dec 11, 2009 IP
  8. vengs

    vengs Greenhorn

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    try this
    $first_comma_out=substr($row['field_with_commas'], 1);
     
    vengs, Dec 11, 2009 IP
  9. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Remove the beginning comma:

    $string = ",1,2,3";
    $string = substr($string, 1, strlen($string)); // Return everything from 1st character to end of string
    PHP:
     
    Wogan, Dec 11, 2009 IP
  10. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    guys i found it & i think its the best solution
    
    UPDATE myTable SET data = IF(data = '','$abc',CONCAT_WS(',',data,'$abc')) WHERE id=1
    
    PHP:
     
    dracula51, Dec 11, 2009 IP
  11. chanceart

    chanceart Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    yes,use the CONCAT_WS
     
    chanceart, Dec 13, 2009 IP