Jump to content

Recommended Posts

Hi,

 

In the page about mysql_real_escape_string on www.php.net there is a 'Best practice method', code using sprintf.  I understand sprintf pretty well now... But I have a question about example from www.php.net, about this section:

 

...

        $query = sprintf("INSERT INTO products (`name`, `description`, `user_id`) VALUES ('%s', '%s', %d)",
                    mysql_real_escape_string($product_name, $link),
                    mysql_real_escape_string($product_description, $link),
                    $_POST['user_id']);

...

 

With this code, it is taking the $product_name and $product_description and escaping it.  But since I need to use these variables in further queries later on, within the same page... I need to have the variables 'raw', that way they don't get escaped twice.  So my question to you, is if I escape the variables using mysql_real_escape_string, have I then changed the variables?  Or am I only escaping the variables for this query, and leaving them 'raw'?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/50406-sprintf-question/
Share on other sites

Thanks for clearing that up... I just wanted to make sure I wasn't escaping the data twice. =)

 

I do have one more question if I may, I am brand new to sprintf(), and I don't really understand the point of it.  I am using it, because all of the examples on php.net use it... So I figure there is something to it, but I don't know what that "something" is.  Could you explain the purpose of it?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/50406-sprintf-question/#findComment-248729
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.