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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:12:06+00:00 2026-06-07T07:12:06+00:00

I have a variable ( $q=$_GET[q]; ) that I want to run my prepared

  • 0

I have a variable ($q=$_GET["q"];) that I want to run my prepared statement with. It contains a column name of my database table.

The value of the variable comes from a dropdown list. When I run the following code the output is the exact value that was chosen in the list. So it is just a column name.

$q=$_GET["q"];

$dsn = "mysql:host=$host;port=$port;dbname=$database";

$db = new PDO($dsn, $username, $password);

$sql = "SELECT DISTINCT ? FROM repertoire";
$stmt = $db->prepare($sql);
$stmt->execute(array($q));

    echo "<select>";
    while ( $row = $stmt->fetchObject() ) {
        echo "<option>";
        echo "{$row->{$q}}";
        echo "</option>";
        }
    echo "</select>";

However, When I change this line $sql = "SELECT DISTINCT ? FROM repertoire";

to $sql = "SELECT DISTINCT ".$q." FROM repertoire"; the I get the desired rows from the database…

I’m not so good with PHP, so I guess that my syntax is wrong somewhere.

Thank you in advance for your help.

  • 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-06-07T07:12:10+00:00Added an answer on June 7, 2026 at 7:12 am

    In PDO, prepared statements prepare the values, not the tables.

    You’ll need to handle the user input and quote directly.

    $q=$_GET["q"];
    
    // Make sure you sanitize your user inputs using filter_inputs() or similar.
    
    $dsn = "mysql:host=$host;port=$port;dbname=$database";
    
    $colNames = new PDO($dsn, $username, $password); // Create object for getting column names.
    $sql = "DESC repertoire Field"; // SQL for getting column names.
    $stmt = $colNames->prepare($sql);
    $stmt->execute();
    
    $colList = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); // Fetch the results into $colList array.
    
    if (in_array($q, $colList)) { // If the value of $q is inside array $colList, then run.
    
        $db = new PDO($dsn, $username, $password);
    
        $sql = "SELECT DISTINCT $q FROM repertoire";
        $stmt = $db->prepare($sql);
        $stmt->execute(array($q));
    
            echo "<select>";
            while ( $row = $stmt->fetchObject() ) {
                echo "<option>";
                echo "{$row->{$q}}";
                echo "</option>";
                }
            echo "</select>";
    }
    

    Also read: Can PHP PDO Statements accept the table or column name as parameter?

    Edit: I’ve added a way to check to make sure $q is a valid column by basically doing a SQL desc in order to get all the column names out of table repertoire.

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

Sidebar

Related Questions

I have a variable which I get from database I want to output this
I have a variable which contains a space-delimited string: line=1 1.50 string I want
Say I have a website with some fields that I want to run some
I have a total variable that I update with a refresh get request using
So I have a php file that retrieves some variables with the $_GET method
I have a variable when i write its value using document.write() i don't get
I have a command I want to run first I ran another command to
I have a python script that I want to debug with python-mode . I
Very simple problem: I have a Public Sub (in a module) that I want
I have a google spreadsheet with multiple columns that have variable row counts in

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.