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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:44:06+00:00 2026-05-12T16:44:06+00:00

I am currently building a query where both the field/column and value parts possibly

  • 0

I am currently building a query where both the field/column and value parts possibly consist of user inputted data.

The problem is escaping the fieldnames.
I’m using prepared statements in order to properly escape and quote the values but when escaping the fieldnames i run into trouble.

  • mysql_real_escape_string requires a mysql connection resource in order to us so that is ruled out
  • PDO::quote adds quotes around the fieldnames which renders them useless in a query too
  • addslashes works but isn’t really safe

Anyone has an idea on what the best way is to properly insert the fieldnames into the query before passing it to PDO::prepare?

  • 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-12T16:44:06+00:00Added an answer on May 12, 2026 at 4:44 pm

    The ANSI standard way of doing a delimited identifier is:

    SELECT "field1" ...
    

    and if there’s a ” in the name, double it:

    SELECT "some""thing" ...
    

    Unfortunately this doesn’t work in MySQL with the default settings, because MySQL prefers to think double quotes are an alternative to single quotes for string literals. In this case you have to use backticks (as outlined by Björn) and backslash-escaping.

    To do backslash escaping correctly, you would need mysql_real_escape_string, because it’s character-set-dependent. But the point is moot, because neither mysql_real_escape_string nor addslashes escape the backquote character. If you can be sure there will never be non-ASCII characters in the column names you can get away with just manually backslash-escaping the ` and \ characters.

    Either way, this isn’t compatible with other databases. You can tell MySQL to allow the ANSI syntax by setting the config option ANSI_QUOTES. Similarly, SQL Server also chokes on double quotes by default; it uses yet another syntax, namely square brackets. Again, you can configure it to support the ANSI syntax with the ‘quoted_identifier’ option.

    Summary: if you only need MySQL compatibility:

    a. use backquotes and disallow the backquote, backslash and nul character in names because escaping them is unreliable

    If you need cross-DBMS compatibility, either:

    b. use double quotes and require MySQL/SQL-Server users to change the configuration appropriately. Disallow double-quote characters in the name (as Oracle can’t handle them even escaped). Or,

    c. have a setting for MySQL vs SQL Server vs Others, and produce either the backquote, square bracket, or double-quote syntax depending on that. Disallow both double-quotes and backslash/backquote/nul.

    This is something you’d hope the data access layer would have a function for, but PDO doesn’t.

    Summary of the summary: arbitrary column names are a problem, best avoided if you can help it.

    Summary of the summary of the summary: gnnnnnnnnnnnh.

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

Sidebar

Related Questions

I am currently building my project locally using MVC. The user enters some search
I'm currently building a website with Django and want to host user bio style
I am currently building a dynamic form that allows the user to add as
I'm currently building a query like this: account.sales.method_needing_more_account_info(account) I want to be able determine
Currently I'm building a system (php and mysql), that on the user profile allows
Or possibly there is a better way. I am building a dynamic query builder
I'm currently building a custom photoalbum but now I'm stuck with a mysql query.
Currently I'm building an windows application using sqlite. In the data base there is
I am building a blog archive navigation menu. Currently I run a query to
I'm currently building SQL queries depending on input from the user. An example how

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.