mysql query to update word in phrase

Discussion in 'MySQL' started by crazy.works, Feb 17, 2009.

  1. #1
    hello,
    i have alot of data in table that i cant update each element alone and i want to change one word in the all elements with one query

    if i have table called "data" , column "column_name", in the "column_name" for example
    phrase = "http://sitename.com/path/1111.jpg" ,
    phrase = "http://sitename.com/path/2222.jpg" ,
    phrase = "http://sitename.com/path/333.jpg" , .......................


    i wanna make the all links http://mysite.com not http://sitename.com and the rest of the paths will stay as it is

    so can i update one word in the all data in that column ??

    thanks
     
    crazy.works, Feb 17, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    use str_replace()
    like
    $text = "http://sitename.com/path/1111.jpg" ;
    $toreplace = "http://sitename.com";
    $yoursite = "urlhere";
    $textreplace = str_replace($toreplace, $yoursite, $text);
     
    crivion, Feb 17, 2009 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    mwasif, Feb 18, 2009 IP
  4. nyxano

    nyxano Peon

    Messages:
    417
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use a MySQL Query and update the database directly...

    UPDATE [table_name] SET [field_name] = REPLACE([field_name],'[string_to_find]','[string_to_replace]');

    ... so for example ...

    UPDATE data SET column_name = REPLACE(column_name,'http://mysite.com','http://sitename.com');
     
    nyxano, Feb 18, 2009 IP
  5. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    okay i have the table called users and the column name called link
    link = "http://sitename.com/path/1111.jpg" , where userid = "1"
    link = "http://sitename.com/path/2222.jpg" , where userid = "2"
    link = "http://sitename.com/path/333.jpg" , where userid = "3"
    ,........... alot of data

    so i executed that query
    UPDATE `users` SET `link` = Replace( `link`, 'http://sitename.com', 'http://mysite.com' );


    and i thought it gonna be like that
    link = "http://mysite.com/path/1111.jpg" , where userid = "1"
    link = "http://mysite.com/path/2222.jpg" , where userid = "2"
    link = "http://mysite.com/path/333.jpg" , where userid = "3"
    ,...........


    but it became like that
    link = "link" , where userid = "1"
    link = "link" , where userid = "2"
    link = "link" , where userid = "3"
    ,...........
     
    crazy.works, Feb 18, 2009 IP
  6. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #6
    The syntax of REPLACE is REPLACE(text_string, from_string, to_string)
     
    crivion, Feb 18, 2009 IP
  7. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks guys , i got it
    the right one is

    UPDATE `table` SET `column` = Replace( `column`, 'http://sitename.com', 'http://mysite.com' );
     
    crazy.works, Feb 22, 2009 IP