Buying need someone to write a mysql query

Discussion in 'Programming' started by schlogo, Apr 6, 2010.

  1. #1
    Hello

    I need a quick fix, i m paypaling 10$ to the one who get it right

    The question is simple, i have this html code in a mysql field, i want to remove it with a mysql query or using the php way, because of the ' and " .

    No time to search, first one who post here the working query gets it :D I have a test database so i can try around

    use thistable as table and thisfield as your field.

    You can either get this code removed of replaced with a br tag
     
    schlogo, Apr 6, 2010 IP
  2. James Barcellano

    James Barcellano Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    I am sorry, what do you need the query to do exactly?
     
    James Barcellano, Apr 6, 2010 IP
  3. flakas123

    flakas123 Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    <?php
    //Your connection above
    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt"));
    mysql_query("DELETE FROM thistable WHERE thisfield='".$text."'");
    ?>

    This might work
     
    flakas123, Apr 6, 2010 IP
  4. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    i am trying in a minute :D
     
    schlogo, Apr 6, 2010 IP
  5. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    flakas123 : PHP Parse error: syntax error, unexpected T_VARIABLE

    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt"));
     
    schlogo, Apr 6, 2010 IP
  6. flakas123

    flakas123 Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    Well, this line is successfuly executed on my localhost :)
    Do you open a connection to your MySQL server?
    If not:
    
    <?php
    @mysql_connect("host", "user", "password");
    @mysql_select_db("database");
    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt")); 
    $query = mysql_query("DELETE FROM thistable WHERE thisfield='".$text."'");
    if(!$query) echo "Query is not complete".mysql_error();
    else echo "Query is complete";
    ?>
    
    Code (markup):
    By the way - you might mistyped the connection before or something like that (maybe forgot a semicolon mark?)
     
    flakas123, Apr 6, 2010 IP
  7. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    query complete but the test is still there
     
    schlogo, Apr 6, 2010 IP
  8. flakas123

    flakas123 Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #8
    Just tested it with your data - query completed, test deleted.

    If it doesn't work - I don't know. Works for me though...
     
    flakas123, Apr 6, 2010 IP
  9. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #9
    mmm maybe because there are other information in the field (that i don t want to be deleted ) you r query is only looking for field equal to the text not containing, is it true?
     
    schlogo, Apr 6, 2010 IP
  10. flakas123

    flakas123 Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #10
    That's correct, the script is looking only for matching fields.
     
    flakas123, Apr 6, 2010 IP
  11. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #11
    
    @mysql_connect("host", "user", "password");
    @mysql_select_db("database");
    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt")); 
    $query = mysql_query("SELECT thisfield FROM thistable WHERE thisfield LIKE "%$text%");
    $delete = mysql_fetch_assoc($query);
    $newText = str_replace($text, "", $delete['thisfield']);
    mysql_query("UPDATE thistable SET thisfield = $nextText WHERE thisfield LIKE "%$text%");
    
    PHP:
    ?>
     
    SHOwnsYou, Apr 6, 2010 IP
  12. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #12
    
    @mysql_connect("host", "user", "password");
    @mysql_select_db("database");
    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt"));
    $query = mysql_query("SELECT primaryfield, textfield FROM thistable WHERE textfield LIKE '%$text%'");
    while($data = mysql_fetch_array($query)){
                $newText = str_replace($text,"", $data['textfield']);
                mysql_query("UPDATE thistable SET thisfield = '$newText' WHERE thisfield='".$data['primaryfield']."'");
    }
    
    PHP:
     
    guardian999, Apr 6, 2010 IP
  13. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #13
    error :/
    [Wed Apr 07 01:53:49 2010] [error] [client 205.151.64.76] PHP Parse error: syntax error, unexpected '"' in /var/www/vhosts/stockportables.fr/httpdocs/db.php on line 2

     
    schlogo, Apr 6, 2010 IP
  14. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #14
    Hello, what is primaryfield ? i have tablename and fieldname that would be your textfield and thistable


     
    schlogo, Apr 6, 2010 IP
  15. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #15
    primaryfield is your unique field, if you don't have it than

    PLEASE BACKUP YOUR DATA FIRST

    Method 1
    
    @mysql_connect("host", "user", "password");
    @mysql_select_db("database");
    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt"));
    $query = mysql_query("SELECT textfield FROM thistable WHERE textfield LIKE '%$text%'");
    while($data = mysql_fetch_array($query)){
                
                  // Temporary Value
                $newText = str_replace($text,"", $data['textfield']);
                 
                 //  Update to current field
                mysql_query("UPDATE thistable SET thisfield = '$newText' WHERE thisfield='".$data['textfield']."'");
       
    }
    
    PHP:
    Method 2
    
    @mysql_connect("host", "user", "password");
    @mysql_select_db("database");
    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt"));
    $query = mysql_query("SELECT textfield FROM thistable WHERE textfield LIKE '%$text%'");
    while($data = mysql_fetch_array($query)){
                
                  // Temporary Value
                $newText = str_replace($text,"", $data['textfield']);
                 
                // Delete the current field
                mysql_query("DELETE FROM thistable WHERE thisfield='".$data['textfield']."'";
         
                // Than new insert to table
                mysql_query("INSERT INTO thistable(thisfield) VALUES ('$newtext')");
    }
    
    PHP:
    Can I see you table structure?
     
    guardian999, Apr 7, 2010 IP
  16. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #16
    Hello, MySQL is not procedural language, so this should be something like that:

    
    error_reporting(0);
    $newText='bla'; //Set your replacement text here.
    mysql_connect("host", "user", "password") or die("MySQL error 1");
    mysql_select_db("database") or die("MySQL error 2");
    $text = file_get_contents("http://www.site.com/test.txt") or die("Error fetching file http://www.site.com/test.txt")
    $text = mysql_real_escape_string($text);
    mysql_query("UPDATE IGNORE TableName SET thisfield=REPLACE(thisfield,'$text','$newText')") or die(mysql_error());
    
    PHP:
    Let me know does this meet your needs.
    Regards :)
     
    koko5, Apr 7, 2010 IP
  17. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #17
    
    @mysql_connect("host", "user", "password");
    @mysql_select_db("database");
    $text = mysql_real_escape_string(file_get_contents("http://www.schlogo.net/test.txt"));
    $query = mysql_query("SELECT `thisfield` FROM `thistable` WHERE `thisfield` LIKE '%$text%'");
    $delete = mysql_fetch_assoc($query);
    $newText = str_replace($text, "", $delete['thisfield']);
    mysql_query("UPDATE `thistable` SET `thisfield` = '$nextText' WHERE `thisfield` LIKE '%$text%');
    
    PHP:
     
    SHOwnsYou, Apr 7, 2010 IP
  18. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #18
    Put a " right before the closing parentheses on the last line. => LIKE '%$text%'");
     
    SHOwnsYou, Apr 7, 2010 IP
  19. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #19
    Hello

    it gives me an error
     
    schlogo, Apr 7, 2010 IP
  20. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #20
    seems to work on the test db, i am trying it live and come back to you :D


     
    schlogo, Apr 7, 2010 IP