mysql syntax error doing search and replace in phpmyadmin

Discussion in 'MySQL' started by Camay123, Nov 30, 2009.

  1. #1
    Im trying to replace an adsense code to wrap it in <center></center> html mark.

    This is the request I am doing :

    UPDATE wp_posts SET post_content = replace(post_content,"<script type="text/javascript"><!--
    google_ad_client = "pub-xxxxxxxxxxxxxxxxxxxx";
    /* 300x250, created 2/20/09 */
    google_ad_slot = "xxxxxxxxxxxxxxxx";
    google_ad_width = 300;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>","<center><script type="text/javascript"><!--
    google_ad_client = "pub-xxxxxxxxxxxxxxxxxxx";
    /* 300x250, created 2/20/09 */
    google_ad_slot = "xxxxxxxxxxxxxxxx";
    google_ad_width = 300;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script></center>");
    Code (markup):
    However, It cannot execute the command as I get a mysql syntax error problem. What could be the problem ?
     
    Camay123, Nov 30, 2009 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    You cannot write php function replace in mysql query.
     
    s_ruben, Nov 30, 2009 IP
  3. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #3
    Write
    UPDATE wp_posts SET post_content = <script type="text/javascript"><!-- google_ad_client = "pub-xxxxxxxxxxxxxxxxxxxx"... WHERE post_content = THE REPLACEMENT;

    THE REPLACEMENT - the content which you want to replace.

    Also pay attention to quotes.
     
    s_ruben, Nov 30, 2009 IP
  4. Camay123

    Camay123 Well-Known Member

    Messages:
    3,423
    Likes Received:
    86
    Best Answers:
    0
    Trophy Points:
    160
    #4
    So, it would be:

    UPDATE wp_posts SET post_content = OLDCONTENT WHERE post_content = NEWCONTENT;

    ?
     
    Camay123, Dec 1, 2009 IP
  5. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #5
    No
    UPDATE wp_posts SET post_content = NEWCONTENT WHERE post_content = OLDCONTENT;
     
    s_ruben, Dec 1, 2009 IP
  6. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    are you sure this will work?
    i would say get a php variable and then use it.
     
    iama_gamer, Dec 3, 2009 IP
  7. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7
    you can do something like $variable = perform the change

    UPDATE wp_posts SET post_content='$variable' WHERE post_content=OLD_CONTENT
     
    iama_gamer, Dec 3, 2009 IP
  8. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #8
    It will work 100% if the quotes of the query is written correctly.
     
    s_ruben, Dec 3, 2009 IP
  9. Camay123

    Camay123 Well-Known Member

    Messages:
    3,423
    Likes Received:
    86
    Best Answers:
    0
    Trophy Points:
    160
    #9
    where would i put these quotes ?
     
    Camay123, Dec 6, 2009 IP
  10. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #10
    $query = "UPDATE wp_posts SET post_content = '<script type=\"text/javascript\"><!-- google_ad_client = \"pub-xxxxxxxxxxxxxxxxxxxx\"...' WHERE post_content = THE REPLACEMENT;"

    Just add \ before every " in NEWCONTENT.
     
    s_ruben, Dec 6, 2009 IP
  11. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #11
    this should work as long as u dont have a ' in the old or the new post content

     
    iama_gamer, Dec 6, 2009 IP