Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-31-2009, 03:35 PM   PM User | #1
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
mressf function

Well I created this function a while back but it's still invaluable to people who generate queries. If you're familiar with sprintf and use it to put your variables into your mysql queries then you can use instead of using mysql_real_escape_string on every param. For example, suppose you have
PHP Code:
$query sprintf("INSERT INTO
`users`
(`firstname`, `lastname`, `address1`, `zipcode`)
VALUES
('%s', '%s', '%s', '%s', '%s')"
,
    
mysql_real_escape_string($firstname), 
    
mysql_real_escape_string($flastname), 
    
mysql_real_escape_string($address1), 
    
mysql_real_escape_string($zipcode)
); 
You could convert it to just
PHP Code:
$query mressf("INSERT INTO
`users`
(`firstname`, `lastname`, `address1`, `zipcode`)
VALUES
('%s', '%s', '%s', '%s', '%s')"
,
$firstname,
$flastname
$address1
$zipcode
); 
and it would escape all the values for you

Here is the function
PHP Code:
function mressf()
{
    
$args func_get_args();
    if (
count($args) < 2)
        return 
false;
    
$query array_shift($args);
    
$args array_map('mysql_real_escape_string'$args);
    
array_unshift($args$query);
    
$query call_user_func_array('sprintf'$args);
    return 
$query;

the sprintf mysql_real_escape_string function can aslo be found here

I hope this is of help to some of you
JAY6390 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:35 PM.


Advertisement
Log in to turn off these ads.