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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:06:09+00:00 2026-05-27T07:06:09+00:00

I am using PDO and want to do something like this: $query = $dbh->prepare(SELECT

  • 0

I am using PDO and want to do something like this:

$query = $dbh->prepare("SELECT * FROM :table WHERE :column = :value");
$query->bindParam(':table', $tableName);
$query->bindParam(':column', $columnName);
$query->bindParam(':value', $value);

Will PDO allow me to bind the table name and the column name like this? It seems to allow it, but it puts quotes around my parameters even if I use PDO::PARAM_INT or PDO::PARAM_BOOL as the data type.

If this won’t work, how can I safely escape my variables so that I can interpolate them in the query?

  • 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-27T07:06:09+00:00Added an answer on May 27, 2026 at 7:06 am

    Unfortunately, you can’t bind parameters by column names.

    What you could try is to dynamically create your SQL command:

    $sql = "SELECT * FROM $tableName WHERE $columnName = :value";
    $query = $dbh->prepare($sql);
    $query->bindParam(':value', $value);
    

    Just make sure to sanitize your parameters/variables if they are coming from elsewhere, to prevent SQL Injection. In this case, $value is safe to a degree but $tableName and $columnName are not — again, that is most especially if the values for these variables are not provided by you and instead by your users/vistors/etc…

    One other thing; please avoid using * and name your columns instead… See some reasons why:

    http://www.jasonvolpe.com/topics/sql/

    Performance issue in using SELECT *?

    See other similar posts here:

    Why doesn't binding parameter in ORDER BY clause order the results?

    How do I set ORDER BY params using prepared PDO statement?

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

Sidebar

Related Questions

I'm using this code and I'm beyond frustration: try { $dbh = new PDO('mysql:dbname='
I want to create a test table that is empty. Using the Example from
I'm having trouble moving from MySQL SELECT methods to PDO methods. I want to
I often see something similar to this below in PHP scripts using MySQL query(SET
I'm using PDO for connect to the database in a system where I want
I'm using PDO to insert a record (mysql and php) $stmt->bindParam(':field1', $field1, PDO::PARAM_STR); $stmt->bindParam(':field2',
while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
I'm using PDO inside a database abstraction library function query that I've made. I'm
I'm loading lots of User and related Group objects from a custom PDO query
I want to populate class with constructor using FETCH_INTO of PDO: class user {

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.