mysql_real_escape_string wrapper problem!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srbakshi
    New Member
    • Aug 2008
    • 18

    mysql_real_escape_string wrapper problem!

    Hey all,
    I'm stuck with the following:

    The mysql_real_esca pe_string(conn, to, from, strlen(from)) function does not return the escaped string. So how can I go about writing a wrapper for it so that it RETURNS the 'to' string which in turn helps me fill out a query in the following manner using sprintf:

    Code:
    unescaped_query = "INSERT into MYTABLE values ('%s', '%s')";
    sprintf(escaped_query, unescaped_query, wrapper(<string argument no.1 that needs to be escaped>, wrapper(<string argument no.2 that needs to be escaped>);
    /* Execute escaped_query */

    You see the problem I'm running into is that the same "wrapper" function is going to be called more than once in a single sprintf statement. That is causing the return value to get overwritten after every call to "wrapper", leading to all values in the table being one and the same (as the return value of the last wrapper function call).

    Can anybody help? :((
    I'm sure this is a common problem and there must be a better and more established way of doing it. Just point me in the right direction.
    Thanks in advance,
    Sid :O)
    Last edited by Atli; May 23 '09, 06:04 AM. Reason: Cleaned up the title.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Originally posted by srbakshi
    The mysql_real_esca pe_string(conn, to, from, strlen(from)) function does not return the escaped string.
    Where did you get that info from?
    According to the manual, this function is described as:
    Originally posted by php.net
    string mysql_real_esca pe_string ( string $unescaped_stri ng [, resource $link_identifie r ] )
    Also, the way you use sprintf makes no sense according to the manual.

    Doing something like this should work fine:
    [code=php]
    $rawSQL = "INSERT into MYTABLE values ('%s', '%s')";
    $formattedSQL = sprintf($rawSQL ,
    mysql_real_esca pe_string($firs tValue)
    mysql_real_esca pe_string($seco ndValue));
    // Execute the formatted SQL[/code]

    Comment

    • srbakshi
      New Member
      • Aug 2008
      • 18

      #3
      Originally posted by Atli
      Where did you get that info from?
      According to the manual, this function is described as:

      Originally Posted by php.net
      string mysql_real_esca pe_string ( string $unescaped_stri ng [, resource $link_identifie r ] )

      Also, the way you use sprintf makes no sense according to the manual.
      Right here: http://dev.mysql.com/doc/refman/5.1/...pe-string.html
      Im working on Sun Solaris and the mysql_real_esca pe_string you talked about does not work.
      So any suggestions now?
      -Sid

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ahh ok.
        As you didn't provide the language you were using, I assumed you were using PHP. (Usually a safe bet.)
        Please try to include facts like these in your posts, so we are not forced to guess.

        Unfortunately I've little experience with C, so I can't really help much.
        I'll move this over to the C/C++ forum. Maybe the experts over there will be able to help.

        Comment

        Working...