Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1084641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:35:20+00:00 2026-05-16T22:35:20+00:00

I am passing a variable to a function that executes a query The MySQL

  • 0

I am passing a variable to a function that executes a query

The MySQL connection only occurs inside the function, and closes inside the function

I want to be able to safely escape strings BEFORE I send them to the function

I can’t use mysql_real_escape_string because it requires a MySQL connection (which is only being made inside the function)

I know the simple answer would be to escape strings inside the function, but I cannot do this because I need to send some escaped, and some non-escaped parts of a string

For example, I need to run the function like this:

myquery("'" . escape_me("My string") . "'");

Notice I am sending two apostrophe’s– unescaped, with an escaped string inside. For this reason I can’t do a blanket mysql_real_escape_string on arguments inside of myquery function.

I found the following code, suggesting that I could use it as an alternative to mysql_real_escape_string:

// escape characters
function escape_me($value) {
    $return = '';
    for($i = 0; $i < strlen($value); ++$i) {
        $char = $value[$i];
        $ord = ord($char);
        if($char !== "'" && $char !== "\"" && $char !== '\\' && $ord >= 32 && $ord <= 126)
            $return .= $char;
        else
            $return .= '\\x' . dechex($ord);
    }
    return $return;
}

I do not know if this function is safe from multibyte attacks, but I think I also need to undo the function every time I query

For example, inputting:
Testing 3’s “OK” is turned into Testing 3x27s x22OKx22 in the database

So my main question is:
Do you know if there is another function I can use as an alternative to mysql_real_escape_string that will safely escape characters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:35:20+00:00Added an answer on May 16, 2026 at 10:35 pm
    • It’s terrible idea to connect every time you’re calling this function. A good planned application wouldn’t have such odd limitation.
    • you can use substitutions, like this
      myquery("SELECT * FROM table WHERE id = %s","My string");

    • You can use another way of substitutions, a modern one: prepared statements. it will be described in numerous other answers.

    as noone posted it yet, here is rough example

    function fetchAll(){
     $args = func_get_args();
     $query = array_shift($args);
     $stmt = $pdo->prepare($query);
     $stmt->execute($args);
     return $stmt->fetchAll();
    }
    $a=$db->fetchAll("SELECT * FROM users WHERE status=? LIMIT ?,?",$status,$start,$num);
    
    • As long as you’re using single-byte encoding or utf-8, no need to use mysql_real_escape_string, so mysql_escape_string(deprecated) or addslashes would be enough
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code doesn't work propely: I call a function passing a variable What's the
I have a simple function that passes a variable var as a u_char array.
Is there a single function that can be created to tell whether a variable
I'm having a problem passing a variable number of parameters to the function: -(void)addCharacterToScene:(NSString
My instructions are to call a function when passing variables into an insert statement
strtok function uses a static variable for parsing the string into tokens. So this
For some unknown reason I'm running into a problem when passing a variable to
My Jquery date selector is passing my date variable in the format 22+Mar+2012 but
I have done urlencode of the variable before passing to the URL http://example.com/Restaurants?alias=F%26B But
I am passing value of text-area in a.cfm in variable of GetXmlHttpObject to b.cfm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.