mysql_real_escape_string and addslashes not working

Discussion in 'PHP' started by sugarland, Aug 25, 2009.

  1. #1
    I tried to use the above 2 functions to escape the string before inserting into the database. But after done, I checked the string in the database, it's not escaped. For example, I inserted "what's your mother's middle name", and escaped with either of the functions, and then use the select * from table_name, and found the string is still "what's your mother's middle name". However, I tried to echo the escaped string after executing one of the 2 functions, it's "what\'s your mother\'s middle name". By the way, I'm testing it on my own windows vista home laptop, with php, mysql, apached installed.

    Anybody knows what's the problem?
     
    sugarland, Aug 25, 2009 IP
  2. a53mp

    a53mp Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What is your sql query? Echo that out
     
    a53mp, Aug 25, 2009 IP
  3. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Check to see that magic quotes isn't enabled, this would cause those extra slashes you have.
     
    premiumscripts, Aug 25, 2009 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    if you're using wamp, magic_quotes_gpc is by default enabled so no need of addslashes()
     
    crivion, Aug 25, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually, there is a need for mysql_real_escape_string. Addslashes (for SQL insertions) is bad and inadequate, it can still be circumvented in some cases. So, what needs to be done is disable magic quotes and use mysql_real_escape_string.

    Obviously you only need to do mysql_real_escape_string, not both.

    Besides, magic quotes is deprecated in php 5.3 and will be removed in php 6, so it's best that you update your scripts now if they rely on this feature.
     
    premiumscripts, Aug 25, 2009 IP
  6. p4n4d0l

    p4n4d0l Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, I agree... actually this is the best function for your problem (ignore my first post) :
    mysql_real_escape_string
     
    p4n4d0l, Aug 26, 2009 IP
  7. ItamarP

    ItamarP Member

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    in order to run those functions , you gotta be logged in to the db
     
    ItamarP, Aug 26, 2009 IP
  8. sugarland

    sugarland Member

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #8
    I'm testing with these small scripts:
    And the output is
    However, if I inserted the escaped $str into the database and then use the "select * from table_name", it's showed:
    What's wrong?
     
    sugarland, Aug 27, 2009 IP
  9. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #9
    When you save the data into mysql database, use this

    $str = mysql_real_escape_string($str);
    PHP:
    When you retrieve the data from mysql and display it out, use this:

    echo stripslashes($data_from_sql);
    PHP:
     
    ads2help, Aug 27, 2009 IP
  10. sugarland

    sugarland Member

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #10
    All, I'm asking why mysql_real_escape_string and addslashes are not working on my windows laptop. The magic quotes is off.

    Can anybody help figure out what's wrong?
     
    sugarland, Aug 28, 2009 IP
  11. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Obviously it's working or your query would not work. In the db, the \ are no longer visible as this is only necessary for the insertion.
     
    premiumscripts, Aug 28, 2009 IP