Results 1 to 12 of 12

Thread: [Resolved] SQL Update/Replace Help

Hybrid View

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] SQL Update/Replace Help

    Let's say I want to run a query that will replace the word dog with cat in all records. How could I accomplish that?

    I know worst case scenario, I could just loop through each record, run str_replace() on it, then update the record with the new text, but I'm hoping there's an easier way?
    Last edited by The Hobo; Aug 3rd, 2003 at 02:42 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Lively Member morrowasted's Avatar
    Join Date
    Aug 2003
    Location
    Houston, TX
    Posts
    118
    unfortunately, no, there isnt an easier way (at least i can't think of any.)

    sorry.

    you'll just have to loop the str_replace().

    of course, they might be able to answer your question here

    (no im not advertising, i just know some very smart PHPers over there)

    -morrowasted

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Bummer. I'll just have to do it that way for now, I guess.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Lively Member morrowasted's Avatar
    Join Date
    Aug 2003
    Location
    Houston, TX
    Posts
    118
    Let me run it by some mates and see what they think, and I'll come back to you with an answer (if there is one)

    -morrowasted

  5. #5
    Lively Member morrowasted's Avatar
    Join Date
    Aug 2003
    Location
    Houston, TX
    Posts
    118
    he says 'try this':
    PHP Code:
    UPDATE tablename SET columnname 'whatever' 

    -morrowasted

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    That would replace every columnname in the record with 'whatever' ...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    columnname = fieldname

    yes that would work hobo. isn't that what you wanted?

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by phpman
    columnname = fieldname

    yes that would work hobo. isn't that what you wanted?
    No.

    Let's say I have a field called 'message' and I want to replace every occurance of the word dog with the word cat.

    So this:

    My dog is a cool dog not like other dogs in the dog town.
    Would become:

    My cat is a cool cat not like other cats in the cat town.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    if this

    "My dog is a cool dog not like other dogs in the dog town."

    is in the field message then you have to query the db twice to get it and replace it. that code morrow gave you will only do one word or a number per field.

    so you have to query the db like so

    select message from table where message like '%dog%'

    then replace all of the them with cat and update the db again with the new line.

    sorry I didn't realize that it was a sentence you wanted.

  10. #10

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    So...I have to do what I refered to as the 'worst case scenario' above? Peachy
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    yeah sorry, I can't think of anything that will do what you want at the moment.

    I was thinking of REPLACE but after looking I don't think that will work either. if it was just a single word in the field it would be easy, but seeing how it is a sentence I don't see any other way.

  12. #12

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Seems like that functionality would exist, but it's no big deal.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width