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 537213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:54:12+00:00 2026-05-13T09:54:12+00:00

I know that mysql_real_escape_string() prepends backslashes to the following characters: \x00, \n, \r, \,

  • 0

I know that mysql_real_escape_string()
prepends backslashes to the following characters: \x00, \n, \r, \, ‘, ” and \x1a

I know how this protects a query from injection into something like a variable in a where clause. But here’s a scenario I am unsure of:

$query = "SELECT * FROM $db WHERE 1";

If $db is taken from a user input, then the user could insert something like:
$db = 'RealDatabase WHERE 1; DELETE FROM RealDatabase WHERE 1; SELECT FROM RealDatabase';

From my understanding, mysql_real_escape_string() would not affect this string,
making the final query:
$query = "SELECT * FROM RealDatabase WHERE 1; DELETE FROM RealDatabase WHERE 1; SELECT FROM RealDatabase WHERE 1";

which would delete the database. Is there another level of protection I am unaware of?

  • 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-13T09:54:12+00:00Added an answer on May 13, 2026 at 9:54 am

    The level of protection you are looking for is supplied by backticks:

    "SELECT * FROM `$db` WHERE 1";
    

    Backticks are used to qualify identifiers that could otherwise be ambiguous (ie. MySQL reserved words), and if you are accepting user input or have variably-named columns or databases, you absolutely should use backticks, or I can promise that you will run into trouble in the future. For example, what if you had a system where a temporary field name was created with some user input, only it turned out the field ended up being named update?

    "SELECT field1,field2,update FROM table;"
    

    It fails miserably. However:

    "SELECT `field`,`field2`,`update` FROM table"
    

    works just fine. (This is actually a real example from a system I worked on a few years ago that had this problem).

    This solves your problem in terms of putting in bad SQL. For instance, the following query will simply return an “unknown column” error, where test; DROP TABLE test is the injected attack code:

    "SELECT * FROM `test; DROP TABLE test`;"
    

    Be careful though: SQL Injection is still possible with backticks!

    For instance, if your $db variable contained data that had a backtick in it, you could still inject some SQL in the normal way. If you’re using variable data for database and field names, you should strip it of all backticks before putting it into your statement, and then qualifying it with backticks once inside.

    $db = str_replace('`','',$db);
    $sql = "SELECT * FROM `$db` WHERE 1";
    

    I utilize a database wrapper which has separate functions for sanitizing data and sanitizing database identifiers, and this is what the latter does 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why do this.. $fruit_type = banana; mysql_real_escape_string($fruit_type); $query = SELECT * FROM posts WHERE
I know that with mysql you can write SQL statements into a .sql file
I know that the following is true int i = 17; //binary 10001 int
Does Microsoft Access have Full Text Search? I know that MySQL and SQL Server
I know that I can do something like $int = (int)99; //(int) has a
I know that default cron's behavior is to send normal and error output to
I know that you can insert multiple rows at once, is there a way
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that .NET is JIT compiled to the architecture you are running on

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.